How can I interpret the JSON returned from jQuery.ajax and using the POST action?

I have the following jQuery code:

$.ajax({ type: "POST", url: "Services/MyService.asmx/Select", dataType: "json", data: "{'data':'test'}", contentType: "application/json; charset=utf-8", success: function(msg){ alert(msg); }, error: function(xhr){ alert(xhr.statusText);} }); 

A method call returns the following:

 "{"FirstName":"James"}" 

When I return my value, my warning returns the full json string. If I try to alert(msg.FirstName) , I get "undefined".

I have seen many examples using the getJSON () method; however, I have not seen a way to use this for the POST verb. Can someone point me in the right direction where I am going wrong? Based on the jquery documentation, the return value should be the same dataType (json) type, so I'm not sure what I am missing.

EDIT: I looked at my service, and these are comparable examples that I find in terms of the signature of the method returning the string. I also confirmed that the response type is / json application.

EDIT2: Updated answer to include external quotes. I also use my own JavaScriptConverter to serialize JSON. A custom converter simply takes my object properties (in this case, FirstName) and loads it, and it adds it to a set of dictionaries that ASP.Net AJAX Extensions v1.0 can easily serialize.

EDIT3: Studying the problem I ran into eval () (it caused the Expected ";" error), I noticed that json property names were also enclosed in quotation marks. As soon as I removed the quotation marks from the property name (not the value), eval () worked again. Now look at the server problem.

+4
source share
5 answers

Using jQuery .ajax looks solid. How do you check the returned data? Firebug? Violinist? Because .asmx web services .asmx not return data like {"FirstName":"James"} . The answers are more like:

 {"d":{"FirstName":"James"}} 

(see http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/ )

For your success callback, try:

 function(res) { alert(res.d.FirstName) } 

Edit : looked at your updates and comments re v1.0 ASP.Net AJAX:

I'm not sure how v1.0 works by serializing your answer, but I assume that if you do your own JSON serialization in your WebService method, the answer might get JSON serialization again. So, you serialize twice.

I believe that all the components you use do what they assume, now only your success callback should be unerialized manually, since you are manually serializing on the server:

 function(res) { res = eval('(' + res + ')'); alert(res.FirstName); } 
+5
source

IIRC you can evaluate the string and the result will be a json object.

myJSON = eval (jsonString);

+2
source

You can define your post variables with a JSON object as the second parameter.

Example:

 $.getJSON("service.py", { foo: bar }, function(data) { $.each(data, function() { // blah }); } ); 

EDIT: I understand what you mean now. Does your service return something / json as a MIME type? If you look at the response headers in Firebug, it should look something like this:

 Content-Type application/json; charset=utf-8 
+1
source

You cannot use $ getJSON with ASMX services serialized through System.Web.Extensions. You are correctly making a call with $ .ajax ().

Have you tried using Firebug to set a breakpoint inside the success handler and check the msg value?

0
source

try something like this:

  result = eval('(' + result.d + ')'); 
0
source

All Articles