任何人都帮助我。我正在使用以下代码在jQuery Mobile中调用Web服务。但是我遇到了“未定义”的错误。请指出我在哪里犯错的地方。提前致谢。

编码:

$.ajax({
type: 'POST',
url: "http://jquery.sample.com/nodes.json",
data: ({search_keys :theName}),
dataType: 'json',
timeout: 5000,
success: function(msg) 
{
   console.log(msg);      //here, I can see the result in browser.  
   alert(msg.message);    //Undefined Error
},
error: function(xhr, status, errorThrown) 
{
alert(status + errorThrown);
}
});      

JSON输出
{“ type”:“ business profiles”,“ title”:“ lakeview餐厅”,“用户”:“ canwest”,“ date”:“ 1280144992”,“ node”:{“ nid”:“ 67916”,“ 67916”,“ type“:“ business_profiles”,“语言”:“”,“ uid”:“ 1”,“状态”:“ 1”,“创建”:“ 1278994293”}}]

有帮助吗?

解决方案

您正在恢复数组,而不是基本对象 - 即使那样 message 我可以看到的财产,因此应该是:

alert(msg[0].title);

或者,遍历所有这些 - 例如:

$.each(msg, function(i, profile) {
  alert(profile.type);
  alert(profile.node.nid);
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top