如何让 Ajax.Net PageMethod 返回 JSON

发布于 2024-08-17 19:25:39 字数 916 浏览 12 评论 0 原文

我正在使用 AJAX.Net 调用 ASP.Net PageMethod,它返回 JSON 序列化 JSON 数据

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

响应标头将内容类型声明为

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

但是,数据仅作为字符串提供,并且似乎不作为 JSON 数据提供来自 JavaScript。我需要做什么才能使用 javascript 返回的 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹梦 2024-08-24 19:25:39
var myData = eval('(' + text + ')');

尽管这可能存在安全风险。相反,您可能想使用 JSON 解析器,例如这个可用的形式 https://github.com/douglascrockford/JSON-js/blob/master/json2.js

然后你会得到如下符号:

var myData = JSON.parse(text);

参见 http://www.json.org/js.html 有关此特定解析器的更多信息...我相信还有其他解析器可供选择,并且它们的工作方式非常相似。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文