Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.getJSON Call ASP.NET method

I have jQuery code to get JSON from the server:

 $(document).ready(function () {
            $.getJSON('Default2.aspx/GetPerson', { 'firstname': 'brian', 'lastname': 'lee' }, function (response) {
                alert(response.Age);
            });    
        });

Default2.aspx code :

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static String GetPerson(String firstname, String lastname)
    {
        Person p = new Person(firstname, lastname);
        return "{\"Age\":\"12\"}";
    }

The question is :

Why GetPerson method is not called from my script? I attach the debugger in GetPerson but it seems doesn't called.

Any help would be appreciate!

like image 925
Iswanto San Avatar asked Nov 25 '13 05:11

Iswanto San


People also ask

How to implement jQuery Ajax call to MVC controller and display JSON?

Follow the below steps to implement jQuery AJAX call to MVC Controller and display JSON result. Open your Visual Studio and create a empty ASP.NET MVC application. Click on File -> New Project -> Web -> ASP.NET web application. From the next window Select template Empty and from Add folders and core reference choose MVC.

How do I get JSON data from an Ajax request?

Get JSON data using an AJAX request, and output the result: $ ("button").click(function() {. $.getJSON("demo_ajax_json.js", function(result) {. $.each(result, function(i, field) {. $ ("div").append(field + " "); }); }); }); Try it Yourself ».

How does jQuery handle multiple callbacks on one request?

The Promise interface in jQuery 1.5 also allows jQuery's Ajax methods, including $.getJSON (), to chain multiple .done (), .always (), and .fail () callbacks on a single request, and even to assign these callbacks after the request may have completed. If the request is already complete, the callback is fired immediately.

How do I get JSONP instead of XMLHttpRequest?

If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax () for more details. As of jQuery 1.5, all of jQuery's Ajax methods return a superset of the XMLHTTPRequest object.


1 Answers

WebMethods by default respond to POST rather than GET requests.

$.ajax({
    type: 'POST',
    url: 'Default2.aspx/GetPerson',
    dataType: 'json',
    // ...
});

And, the request format should be JSON as well to match the ResponseFormat:

// ...
    data: JSON.stringify({ 'firstname': 'brian', 'lastname': 'lee' }),
    contentType: 'application/json'

Alternatively, a ScriptMethod can be configured to use GET instead:

[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]

Though, contentType still needs to be set for it, so $.getJSON() can't be used:

$.ajax({
    type: 'GET',
    url: 'Default2.aspx/GetPerson',
    dataType: 'json',
    contentType: 'application/json',
    // ...
});

And, data will be URL-encoded, but each value will need to be JSON-encoded before that:

// ...
    data: {
        firstname: JSON.stringify('brian'),
        lastname: JSON.stringify('lee')
    }

Also note that ScriptMethods will wrap their response in a { "d": ... } object. And, since the return value is a String, the value of "d" be that same unparsed String:

// ...
    success: function (response) {
        response = JSON.parse(response.d);
        alert(response.Age);
    }
like image 177
Jonathan Lonowski Avatar answered Oct 13 '22 10:10

Jonathan Lonowski