使用php编码变量调试不可见的JSON错误,使用jQuery解析

时间:2013-12-21 19:14:19

标签: javascript php jquery json

我正在使用php json_encode向客户端发送一堆值,其中jquery

{"data":{"application":{"basics":{"email":"jepp@mily.com","name_first":"Step","name_last":"Bob","phone":"9210938102938","occupation":"unemployed","website":"wwwcom"},"application":{"title":"Default title","guid":"9as8ua9sd8ua9sdu","language":"sv"},"job":{"title":"joburl","url":"joburl"},"letter":{"letter":"abadbabkbakbakbakbakbakbabk"},"work-experiences":[{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"},{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"},{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"}],"educations":{"title":"Default Education","url":"ixi","date_from":"1970-01-01","date_to":"1999-12-31"},"skills":{"title":"Defailt SKILL","url":"wwwsdcom","date_from":"ixi","date_to":"ixi"},"work-samples":{"title":"A sample of my work","url":"wwwsdcom","date_from":"ixi","date_to":"1999-12-31"}}},"error":[],"warning":[]}

如果我尝试在脚本中用$.parseJSON解析它,我就没有对象。但是,如果我将其直接复制/粘贴到控制台中(并在开头和结尾添加'),它就可以了。没有错误代码,我看不到换行符。 JSON lint-tools不返回错误......

我已经设置了正确的内容类型并尝试了jquery提供的不同json解析器。

我错过了什么?


代码是从jQuery教程剪切/粘贴的。我尝试了一些不同的例子,但都失败了。

var jqxhr = $.getJSON( "application_controller.php", function() {
    console.log( "success" );
})
.done(function() {
    console.log( "second success" );
})
.fail(function() {
    console.log( "error" );
})
.always(function(data) {
    console.log( "complete" );
    application = data;  
});

// Perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function() {
    console.log( "second complete" );
});
});

这是真的,我在控制台中解码了它。这是一个在脚本中执行的片段(它也不起作用):

$.ajax({
  dataType: "json",
  contentType: "application/json",
  url: 'application_controller.php',
  data: '{id:id}',
  success: function( data ) {
        application = data.responseText;
        application = $.parseJSON(application);/* < string */     
    },
    fail: console.log("fail"),
    complete: function(data) {
        console.log(data.responseText);
        application = data.responseText;
    }
});

2 个答案:

答案 0 :(得分:1)

回答我自己的问题:

要解决此错误,我必须将所有php文件从“utf8”编码更改为“utf8 without BOM”。然后它奏效了。

当包含没有utf8编码的文件时(当然层次结构中较低),它会污染所有其他文件并破坏输出。

答案 1 :(得分:0)

使用JSON.parse(jsonString)函数。

var jsonString = '{"data":{"application":{"basics":{"email":"jepp@mily.com","name_first":"Step","name_last":"Bob","phone":"9210938102938","occupation":"unemployed","website":"wwwcom"},"application":{"title":"Default title","guid":"9as8ua9sd8ua9sdu","language":"sv"},"job":{"title":"joburl","url":"joburl"},"letter":{"letter":"abadbabkbakbakbakbakbakbabk"},"work-experiences":[{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"},{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"},{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"}],"educations":{"title":"Default Education","url":"ixi","date_from":"1970-01-01","date_to":"1999-12-31"},"skills":{"title":"Defailt SKILL","url":"wwwsdcom","date_from":"ixi","date_to":"ixi"},"work-samples":{"title":"A sample of my work","url":"wwwsdcom","date_from":"ixi","date_to":"1999-12-31"}}},"error":[],"warning":[]}';

var myData = JSON.parse(jsonString);

Fiddle