Monday, April 16, 2012

Getting JSON Object Result from $.post()

I'm attempting to call a web service via AJAX in a WebForms application.



My script looks something like this:



$.post('UpdateServer.asmx/ProcessItem',
'itemId=' + $(this).text(),
function (result) {
alert(result);
});


My web service looks something like this.



[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class UpdateServer : System.Web.Services.WebService
{
[WebMethod]
public string ProcessItem(int itemId)
{
return new JavaScriptSerializer().Serialize(
new { Success = true, Message = "Here I am!" });
}
}


The web method is called as expected and with the expected argument. However, the argument passed to my success function (last parameter to $.post()) is of type document and does not contain the Success and Message members that I'm expecting.



What's are the magic words so that I can get back the object I'm expecting?



EDIT



On closer inspection, I can find the data I'm looking for as follows:




result.childNodes[0].childNodes[0].data:
"{"Success":true,"Message":"Server successfully updated!"}"






No comments:

Post a Comment