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!
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.
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 ».
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.
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.
WebMethod
s 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 ScriptMethod
s 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With