JSON - msg.d 是未定义的错误

发布于 2024-08-29 08:20:54 字数 942 浏览 1 评论 0原文

Webmethod 返回一个对象数组 - 类似这样

{"d":
[[{"Amount":100,"Name":"StackOverflow"},
{"Amount":200,"Name":"Badges"},
{"Amount":300,"Name":"Questions"}]]}

在客户端,当使用 msg.d 引用 JSON 时,我收到错误

msg.d is undefined 

。我正在使用 jQuery JavaScript Library v1.4.2

如何访问对象数组中的元素?


添加更多发现、代码和问题:

  1. 我在返回的 JSON 对象中没有看到 __type。这是否意味着从服务器发送的对象不是 JSON 格式的?
  2. 当 __type 不是响应的一部分时,我将无法使用 msg.d? (msg.d 未定义)

更多: 1.我可以使用以下方式从客户端访问元素 msg[0][0].Amount - 如何专门 JSON 格式化我的返回对象(从服务器)


代码 调用页面方法

PageMethods.BuildParticipantAsync($get('<%=hdn_AjaxControls.ClientID %>').value, fOnSuccess, fOnFailure);

function onSuccess(msg)
{
alert(msg.d); //This is undefined
}

Web Method
public static object[] BuildParticipantAsync(string lstSAjaxControls)
{
...//do stuff
 return new object[] { ArrayOfObject };
}

Webmethod returns an array of objects - something like this

{"d":
[[{"Amount":100,"Name":"StackOverflow"},
{"Amount":200,"Name":"Badges"},
{"Amount":300,"Name":"Questions"}]]}

On the client-side, when the JSON is referenced using msg.d, I get a

msg.d is undefined 

error. I am using jQuery JavaScript Library v1.4.2

How do I access the elements in the array of objects?


Adding more findings, code and questions:

  1. I don't see __type in the JSON object that is returned. Does that mean that the object sent from the server is not JSON formatted?
  2. When the __type is not a part of the response, I will not be able to use msg.d? (msg.d is undefined)

Some more:
1. I can access the elements from client side using
msg[0][0].Amount - How can I specifically JSON format my return object (from the server)


Code
Call to the PageMethods

PageMethods.BuildParticipantAsync($get('<%=hdn_AjaxControls.ClientID %>').value, fOnSuccess, fOnFailure);

function onSuccess(msg)
{
alert(msg.d); //This is undefined
}

Web Method
public static object[] BuildParticipantAsync(string lstSAjaxControls)
{
...//do stuff
 return new object[] { ArrayOfObject };
}

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

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

发布评论

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

评论(1

陈年往事 2024-09-05 08:20:54

伙计们,这就是我发现的。因为,我返回的是一个 object[] 而不是类的复合对象。我将无法使用 msg.d[0].Amount 进行引用(正如我认为的那样)。我将不得不使用 msg[0][0].Amount - 在某种程度上,它似乎确实很有意义。

Guys, here's what I found. Because, I am returning back a object[] and not a composite object of a class. I will not be able to reference using msg.d[0].Amount (as I thought I would be able to). I will have to use msg[0][0].Amount - In a way, it does seem to make a lot of sense.

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