Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

jquery - Ajax GET requests to an ASP.NET Page Method?

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&param2=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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For security reasons, ASP.Net AJAX page methods only support POST requests.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...