A situation I ran across this week: we have a jQuery Ajax call that goes back to the server to get data
$.ajax(
{
type: "POST",
contentType: "application/json; charset=utf-8",
url: fullMethodPath,
data: data,
dataType: "json",
success: function(response) {
successCallback(response);
},
error: errorCallback,
complete: completeCallback
});
fullMethodPath is a link to a static method on a page (let’s say /MyPage.aspx/MyMethod).
public partial class MyPage : Page
{
// snip
[WebMethod]
public static AjaxData MyMethod(string param1, int param2)
{
// return some data here
}
}
This works, no problem.
A colleague had attempted to replace this call with one where type was “GET”. It broke, I had to fix it. Eventually, I went back to POST because we needed the fix quick, but it has been bugging me because semantically a GET is more “correct” in this case.
As I understand it, jQuery translates an object in data to a Query String: /MyPage.aspx/MyMethod?param1=value1¶m2=value2 but all I could get back was the content of the page MyPage.aspx.
Is that just a “feature” of Page methods, or is there a way of making a GET request work?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
For security reasons, ASP.Net AJAX page methods only support POST requests.
Method 2
It is true that ASP.NET AJAX page methods only support POST requests for security reasons but you can override this behavior by decorating your WebMethod with this these both attribute:
[WebMethod] [ScriptMethod(UseHttpGet = true)]
I felt that the accepted answer was incomplete without pointing out a work around.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0