[英]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)
}
});
解析ur數據變量時未解析json變量的原因是因為json變量具有換行符(\\ n)。
由於您的數據變量在一行中包含字符串,即沒有換行符,因此該函數成功解析了該字符串。
我可能還應該指出,提供給JSON解析函數的字符串中的諸如tab(\\ t)之類的字符或任何其他轉義字符都將導致該函數失敗。
解析錯誤的字符串var:
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/
檢查上面的鏈接以獲取更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.