Question

I am using an AJAX.Net to call an ASP.Net PageMethod, which returns JSON serialized JSON data

{"d":"[{\"Fromaddress\":\"testfrom1@test.com\",\"Toaddress\":\"testto1@test.com\"},{\"Fromaddress\":\"testfrom2@test.com\",\"Toaddress\":\"testto2@test.com\"}]"}

The Response Header states the content type as

"Content-Type   application/json; charset=utf-8"

However, the data is just available as a string, and does not seem to be available as JSON data from javascript. What do I need to do to work with the returned data as JSON from javascript?

Was it helpful?

Solution

var myData = eval('(' + text + ')');

Although this can be a security risk. Instead you might want to use a JSON parser, such as this one available form https://github.com/douglascrockford/JSON-js/blob/master/json2.js

Then you get notation like:

var myData = JSON.parse(text);

See http://www.json.org/js.html for more info on this particular parser... I believe there are others to choose from, and that they work very similarly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top