2

当我进行以下 jQuery 调用时

$.post('/publish/comments', 
  parms,
  function(data, textStatus){
    console.log('textStatus: ' . textStatus);
    console.log('returned data: ' . data);
    console.log('nextPage: ' . data.nextPage);
  },
  "json"
); 

控制台显示 data、textStatus 和 data.nextPage 都是未定义的。

但在 Chrome 开发工具(网络选项卡)中,我可以看到以下 JSON 数据正在从服务器传回。

{success:true, nextPage: /go/page/requestId/182/src/publish}
nextPage: "/go/page/requestId/182/src/send"
success: true

想法?

4

3 回答 3

7

正如 Felix 指出的那样,连接需要“+”

另外,我发现如果你想记录整个对象,你不想在你的 console.log() 中包含字符串。

尝试console.log(data)不要在其前面放置字符串(如“数据:”),这将保证可以在控制台中看到整个对象。

于 2012-05-11T15:30:21.013 回答
3

您需要使用+运算符而不是.php 中使用的运算符来连接

$.post('/publish/comments', 
       parms,
       function(data, textStatus){
                            console.log('textStatus: ' + textStatus);
                            console.log('returned data: '+ data);
                            console.log('nextPage: ' + data.nextPage);    
       },
       "json"
       ); 
于 2012-05-11T15:30:38.613 回答
1

尝试使用此 Code Pattern 登录到控制台:

console.log(['textStatus', textStatus]);

这允许将消息和对象打印到控制台。

That said, 'textStatus: ' . textStatus is undefined because JavaScript strings don't have a property textStatus (. is the "access property" operator not the PHP concat operator)

于 2012-05-11T15:33:46.923 回答