Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 896
pdr Avatar asked Mar 07 '10 18:03

pdr


People also ask

Can we use AJAX in ASPX page?

AJAX in ASP.NET WebFormsThe method needs to be public, static, and add an attribute as WebMethod on top of it. Add the following code in code-behind file (*. aspx. cs) which receives list of employees and returns same.

How can we call ASPX CS from AJAX in asp net?

Cs Page Using Ajax. $(function () { try { debugger; $. ajaxSetup({ async: false }); setInterval(function () { CheckSession(); }, 10000); } catch (exception) { manageOverlay(false); alert(exception); } });

What is PageMethod in AJAX?

PageMethod takes country as input parameter which is mentioned with data property of $. ajax. success: function (data) { } will execute when PageMethod executes successfully. It add row for each customer record.

Can we use get in AJAX?

GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data. POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.


1 Answers

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

like image 137
SLaks Avatar answered Sep 21 '22 15:09

SLaks