1

我正在使用带有以下函数调用的 ajax 请求。

var test = initBuild(id);
    test.success(function (data){
    console.log(data);
});

被退回的data样子

{"built_when":{"id":"43701","clientId":"245","name":"Big Ass Gallery","productId":"0","desc":null,"bgColor":"#000000","fontColor":"ffffff","sort":"43701","clientRef":"205","isFeatured":"0","created":"1367356190","views":"0","finish":"0","isArchive":"0","showMailShare":"0","hardPageFlip":"0","hardCoverFlip":"0","isTemp":"0","agreement":"0","maxChange":"0","countChange":"0","verticalFlip":"0","musicSwitch":"0","showFbShare":"0","twitter":null,"email":"0","pageHeight":"1000","pageWidth":"1391"}} 

我正在尝试通过 data.built_when.id 访问,但它返回 null。我在这个上缺少什么。

谢谢

4

2 回答 2

2

它作为字符串返回。用于data = $.parseJSON(data);将其转换为 JS 构造。或者,您可以Content-type: application/json在发出 JSON 时在服务器端设置标头

于 2013-05-01T03:00:44.077 回答
1

如果您确定 ajax 请求的返回类型始终为,json那么您可以设置 dataType: 'json'为 ajax 选项。

前任:

$.ajax({
    url: '',
    dataType: 'json',
    ....
})
于 2013-05-01T03:11:36.623 回答