James
James

Reputation: 7897

How to get Ajax.Net PageMethod to return JSON

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

{"d":"[{\"Fromaddress\":\"[email protected]\",\"Toaddress\":\"[email protected]\"},{\"Fromaddress\":\"[email protected]\",\"Toaddress\":\"[email protected]\"}]"}

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?

Upvotes: 3

Views: 1951

Answers (1)

wprl
wprl

Reputation: 25387

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.

Upvotes: 4

Related Questions