Skip to content

Instantly share code, notes, and snippets.

@gon250
Created July 8, 2015 13:49
Show Gist options
  • Save gon250/1c96e42bbeb3776705bb to your computer and use it in GitHub Desktop.
Save gon250/1c96e42bbeb3776705bb to your computer and use it in GitHub Desktop.
Base Ajax Call
//My answer Stackoverflow
// http://stackoverflow.com/a/30301335/2545964
function baseAjaxCall(option, sCb) {
var ajaxOptions = {
method: option.method || "GET",
url: option.url,
dataType: option.dataType || "xml",
success : function(data) {
var root = $($.parseXML(data)).find("Response");
if (root.children("Type").text() == "Error") {
toastr.error(root.children("Content").text(), "Error " + root.children("ReturnCode").text());
return;
}
else {
sCb(root);
}
},
error : function(qXHR, textStatus, errorThrown){
toastr.error(errorThrown, "Error " + qXHR.status);
}
};
//you can check for optional settings
if(option.contentType !== undefined){
ajaxOptions.contentType = option.contentType;
}
$.ajax(ajaxOptions);
}
baseAjaxCall({ url: "someurl.html" }, function(root){
// no need to chek for errors here!
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment