My team and I have been struggling with failed jQuery Ajax requests on certain pages of our ASP.NET website, while other pages have successful requests.
Here’s my jQuery code:
$.ajax({
url: 'Test.aspx/Hello',
data: '{"name": "alex"}',
dataType: 'json',
type: 'POST',
contentType: 'application/json; charset=utf-8',
error: function() { console.log("error"); },
success: function(data) { console.log(data.d); }
});
And the C# code (in Test.aspx.cs):
[WebMethod]
public static string Hello(string name)
{
return "success: " + name;
}
This will sound bizarre, but it seems that the problem occurs because the folder containing the pages with the failed requests is called “Adverts”.
I’ve moved the page with the WebMethod (Test.aspx/Test.aspx.cs) to many different folders of my project. I can always call the WebMethod through jQuery Ajax, except when I move the page into the Adverts folder. I’ve gone as far as renaming the Adverts folder to “Jeffrey”. Immediately, the Ajax requests completed successfully. I then created another folder called “Adverts” and moved the page with the WebMethod in there. Strangely, the requests failed.
I’ve tried calling the WebMethod from the same page, like the example above, as well as different pages, like the example below:
url: '/Adverts/Test.aspx/Hello',
I don’t know what else to do other than not use a folder called Adverts, but that feels like quitting. Can anyone please shed any light on this?
Maybe you have some Advertisement-blocking extesions in your browser?
Also you can use “Incognito mode” while debugging.