JSON - msg.d 是未定义的错误
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
如何访问对象数组中的元素?
添加更多发现、代码和问题:
- 我在返回的 JSON 对象中没有看到 __type。这是否意味着从服务器发送的对象不是 JSON 格式的?
- 当 __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:
- 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?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
伙计们,这就是我发现的。因为,我返回的是一个 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.