0

我有一些通过 jquery$.post()请求从 php 代码中检索到的 json 数据。格式为 json。但是调用回调函数时,我的浏览器上没有处理数据

$.post(url_send, data_to_send, function(callback_data){
      var info = callback_data.info;
      console.log(callback_data.info);
      ...
});

callback_data = {"success":[{"id":some_number}],"info":"some_string"} 

为了调试,我注意到:如果我输入这段代码console.log(callback_data);,正确的 json 数据会显示在我的控制台中,但如果我让console.log(callback_data.info),控制台显示'undefined'. 有人可以解释一下吗?

编辑:在我的 php 中,我有 $reply = array('success' => $ids,'info' => $info); 回声 json_encode($reply);

4

3 回答 3

2

您的 php 页面返回的 json 可能不包含名为“info”的字段。因此,当您尝试时callback_data.info,由于没有信息,因此它是未定义的。

于 2013-02-01T16:23:21.407 回答
2

callback_data 将是字符串格式,您必须使用 eval 将其转换为 json 才能将其用作对象。一旦执行 eval,您应该有 callback_data.info 类似这样的内容

$.post(url_send, data_to_send, function(callback_data){
      callback_data = eval('(' + callback_data + ')');
      console.log(callback_data.info);
      ...
});
于 2013-02-01T16:25:57.033 回答
0

该字段为信息,应为:callback_data.information

于 2013-02-01T16:25:13.637 回答