jQuery.parseJSON不工作,除非复制到一个不同的字符串变量
jQuery.parseJSON not working unless copied in a different string variable
我对javascript和jQuery相当陌生。我很确定我在这里错过了一些简单的东西。下面的代码将输出:
[{
"cli_surname":"'u0392'u03bf'u03bd'u03b1'u03c0'u03ac'u03c1'u03c4'u03b7'u03c2",
"cli_name":"'u039d'u03b1'u03c0'u03bf'u03bb'u03ad'u03c9'u03bd",
"cli_sex":"M",
"cli_dob":"1769-08-15",
"cli_insurance":"1",
"cli_phone":"9999999999",
"cli_mobile":"9999999999",
"cli_email":"bonaparte@hotmail.com",
"cli_address":"'u0392'u03b1'u03c4'u03b5'u03c1'u03bb'u03ce 18",
"ct_name":"'u0391'u03b3'u03af'u03b1 'u0392'u03b1'u03c1'u03b2'u03ac'u03c1'u03b1",
"cli_comments":"'u039c'u03ad'u03b3'u03b1'u03c2"
}]
到$("#userInfo")
,但jQuery。parseJSON将失败。
$.ajax({
url: url,
type: "POST",
data: "c=" + selected.val(),
success: function (json) {
$("#userInfo").html(' ' + json);
var obj = jQuery.parseJSON( json );
alert( obj[0].cli_name );
},
error: function (xhr) {
$("#userInfo").html('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
}
});
现在,如果我从$("#userInfo")
复制上述代码的输出,并将其粘贴到一个名为'data'的新变量中并解析它,它就可以工作了。
$.ajax({
url: url,
type: "POST",
data: "c=" + selected.val(),
success: function (json) {
$("#userInfo").html(' ' + json);
data = '[{"cli_surname":"'u0392'u03bf'u03bd'u03b1'u03c0'u03ac'u03c1'u03c4'u03b7'u03c2","cli_name":"'u039d'u03b1'u03c0'u03bf'u03bb'u03ad'u03c9'u03bd","cli_sex":"M","cli_dob":"1769-08-15","cli_insurance":"1","cli_phone":"9999999999","cli_mobile":"9999999999","cli_email":"bonaparte@hotmail.com","cli_address":"'u0392'u03b1'u03c4'u03b5'u03c1'u03bb'u03ce 18","ct_name":"'u0391'u03b3'u03af'u03b1 'u0392'u03b1'u03c1'u03b2'u03ac'u03c1'u03b1","cli_comments":"'u039c'u03ad'u03b3'u03b1'u03c2"}]';
var obj = jQuery.parseJSON( data );
$("#userInfo").html(" " + data);
alert( obj[0].cli_name );
},
error: function (xhr) {
$("#userInfo").html('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
}
});
对两个变量同时应用typeof,结果为string
。到底发生了什么事?它是如何,我可以解析data
变量,而不是json
变量,因为它们都是字符串,似乎包含相同的数据?
Ajax调用将在内部为您进行解析。
所以,你的参数json
到你的成功函数已经包含一个有效的javascript对象,这就是为什么解析失败:它是一个对象,而不是一个字符串。
构造$("#userInfo").html(' ' + json);
然后将json再次转换为字符串,这就是为什么你在div中看到适当的内容。
改变:
$.ajax({
url: url,
type: "POST",
dataType: 'json', // this will force the response to be Json
// even if MIME-Type tells something different!
data: "c=" + selected.val(),
success: function (json) {
$("#userInfo").html(' ' + json);
// unnecessary var obj = jQuery.parseJSON( json );
alert( json[0].cli_name );
},
error: function (xhr) {
$("#userInfo").html('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
}
});
刚刚添加了dataType,以便在本地覆盖错误的MIME-Type响应头。根据第一个答案
试试这个:
响应将自动解析为json
$.ajax({
url: url,
type: "POST",
data: "c=" + selected.val(),
dataType: 'json', // this param talk that response will be parse automatically
success: function(data) {
console.log(data)
}
});
你的json变量没有被解析,而你的数据变量正在被解析的原因是你的json变量有新的行字符('n)。
由于您的数据变量在一行中包含字符串,也就是说,没有新的行字符,该函数成功地解析了字符串。
我可能还应该指出,提供给JSON解析函数的字符串中的tab ('t)或任何其他转义字符将导致函数失败。
解析错误的字符串变量:
var myVar = "This is a text"; //will fail due to new line characters.
var myVar = 'My Friend''s birthday'; //will fail due to the "'" before "'"
var myVar = ' This is a text.' //will fail due to the tab character at the beginning of the line
" JSON标准不允许"控制字符",如制表符或换行符。"——http://api.jquery.com/jquery.parsejson/
查看上面的链接获取更多信息
- 导航到特定事件的另一个变量页面
- 我可以在Javascript/jQuery中使用一个变量作为键吗
- 使用mongodb更新中的一个变量
- 将节点数据分配给另一个变量jstree
- Javascript:在激发函数后返回一个变量以供使用
- 替换另一个变量对象中的变量值
- 什么'它的意思是当一个变量位于括号外时
- 可以't将sessionStorage设置为Javascript中的一个变量
- 在Flickr API的标签参数中传递一个变量
- 从Jquery函数中获取一个变量
- 为两个ID设置一个变量的正确语法
- 如何将变量值附加到另一个变量
- 通过加载函数发送一个变量
- 如何将一个变量从一个控制器传递到合金中的另一个控制器
- 如何在Jquery函数中为自动完成文本区域指定一个变量作为Id
- Javascript未在其中一个变量上输出0
- 保留“;这个“;在一个变量中;这个“;使用此变量
- 是否可以在另一个变量中使用未声明的变量
- JS/ANGULAR使用一个变量's值作为另一个对象的键
- 如何每秒从一个变量中提取一定次数的javascript/html