I am trying to execute a PageMethod in aspx page using jQuery from a html page, which works fine on my local machine. But when I deploy the web page to a remote production web server, then I get a 401 Unauthorized Error when the ajax request executes. My code is as below.
$.ajax({
type: 'POST',
url: 'SupplierMethods.aspx/GetAgeSummaryForPendingDocuments',
data: "{ 'supplierId': '" + vid + "'}",
dataType: 'json',
timeout: 180000, //3 minutes is timeout for this ajax request
contentType: "application/json; charset=utf-8",
beforeSend: function () {
},
success: function (json) {
stats = json.d.StatsBuckets;
},
error: function (xhr, textStatus, errorThrown) {
alert('An error occurred! ' + (errorThrown ? errorThrown :
xhr.satus));
}
});
});
UPDATE : My problem was due to something very trivial. I copied over the website to remote server, but forgot to copy over the dll’s. the PageMethod is compiled into a dll in ASP.Net website, and because it was missing, so the call to a PageMethod through jQuery ajax came back with an error message. So it seems jQuery API for AJAX are as good as can be.
I was getting this 401 error because the new dll in ASP.Net website that contained the PageMethod I was calling using jQuery ajax, was not deployed to remote server. So the moral of the story is – ALWAYS deploy your website with care so you don’t miss out on any files.
And also make sure your syntax for calling ajax in jQuery adheres to the syntax.