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);}
});
The call to the method returns the following:
"{"FirstName":"James"}"
When I get the value back, my alert returns the full json string. If I try to do alert(msg.FirstName)
, I get "undefined".
I have seen a lot of examples using the getJSON() method; however, I haven't seen a way to use that for a POST verb. Can anyone point me in the right direction where I'm going wrong? Based on the jquery documentation, the return value should be the same dataType (json) so I'm not certain what I'm missing.
EDIT: I looked on my service and it is matching examples that I'm finding in terms of the method signature returning a string. I've also confirmed the response type is of application/json.
EDIT2: Updated the response to include the outside quotes. I'm also using a custom JavaScriptConverter to do the JSON serialization. The custom converter just takes my object properties (in this case FirstName) and loads it and it's value into a Dictionary collection which the ASP.Net AJAX Extensions v1.0 can serialize easily.
EDIT3:
Looking into the issue that I was having with eval() (it caused an Expected ";"
error), I noticed that the json property names were also enclosed in quotes. Once I removed the quotes from the property name (not the value), eval() worked again. Looking into the server side of this issue now.