Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
i am not sure if that is the reason, you could give the property name as data. data:{ 'firstname': 'brian', 'lastname': 'lee' } –  Runner Nov 25 '13 at 5:14
    
No, it doesn't work –  Iswanto San Nov 25 '13 at 5:15
    
stackoverflow.com/questions/16910982/…. i guess you need to get rid of the webmethod.as per this post, webmethods are obsolete –  Runner Nov 25 '13 at 5:26
    
I am using ASP.NET, not ASP MVC..:) –  Iswanto San Nov 25 '13 at 5:34
add comment

1 Answer

up vote 4 down vote accepted

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);
    }
share|improve this answer
    
You said WebMethods by default respond to POST rather than GET requests.. So how can i set WebMethod respond to GET? –  Iswanto San Nov 25 '13 at 6:14
    
@IswantoSan It is possible, though with some caveats. See my edit. –  Jonathan Lonowski Nov 25 '13 at 6:54
    
Thank you so much! –  Iswanto San Nov 25 '13 at 7:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.