我正面临与 json 相关的非常奇怪的 jQuery 行为。以下是我的代码,其中我向发送 JSON 响应的 servlet 发送 POST 请求。我能够检索大多数请求的值,但对于某些请求,我在从 JSON 检索值时收到以下错误:
parsererror, SyntaxError: JSON.parse: bad control character in string literal
但是当我在这个站点上检查 JSON 响应(我从 Eclipse 控制台获取它)时:在此处输入链接描述
这个站点解析 JSON 没有给出任何错误!!转到站点-> 在“文本”选项卡下的文本区域中复制任何 JSON,然后单击“查看器选项卡”。它会正确解析它。
以下是 2-3 个 JSON,jQuery 报告错误 -
{"topics": [{ "categoryName":"Law Crime" , "score":"90%"}],"socialTags": [{ "originalValue":"Social inequality" , "importance":"1"},{ "originalValue":"Affirmative action" , "importance":"1"},{ "originalValue":"Discrimination" , "importance":"1"},{ "originalValue":"Education policy" , "importance":"2"},{ "originalValue":"Politics" , "importance":"2"},{ "originalValue":"Ethics" , "importance":"2"},{ "originalValue":"Social philosophy" , "importance":"2"},{ "originalValue":"Same-sex marriage in Canada" , "importance":"2"},{ "originalValue":"Affirmative action in the United States" , "importance":"2"},{ "originalValue":"Same-sex marriage in the United States" , "importance":"2"},{ "originalValue":"Law Crime" , "importance":"1"}],"entities": [{ "_type":"Facility" , "name":"Supreme
Court"},{ "_type":"Organization" , "name":"Supreme
Court"}]}
我已经尝试了很多次,每次对于这些 JSON,我都会遇到同样的错误。
我在 servlet 的后端创建这些 JSON。
以下是我在 jQuery 中从 JSON 检索值的代码:
$.ajax({
type: 'POST',
//servlet url
url: 'calaiscaller',
// parameters
data: {content: summary},
// expected data-type of response
dataType: 'json',
// to execute when got the json result
success: function(jsonResponse){
// clear the old topic data
$('#topics').empty();
$('#topics').append("<p class='text-left label label-info'>Topics:</p>");
// add new topic data
$.each(jsonResponse.topics, function(){
var topicData="<p><span class='text-left'>" + this.categoryName + "</span><span class='pull-right'>" + this.score + "</span></p>";
$('#topics').append(topicData);
});
// clear new social-tag data
$('#social-tags').empty();
$('#social-tags').append("<p class='text-left label label-info'>Social Tags:</p>");
// add new social-tag data
$.each(jsonResponse.socialTags, function(){
var socialTagData="<p><span class='text-left'>" + this.originalValue + "</span><span class='pull-right'>" + this.importance + "</span></p>";
$('#social-tags').append(socialTagData);
});
// clear new entities data
$('#entities').empty();
$('#entities').append("<p class='text-left label label-info'>Entities:</p>");
// add new entities data
$.each(jsonResponse.entities, function(){
var entitiesData="<p><span class='text-left'>" + this._type + "</span><span class='pull-right'>" + this.name + "</span></p>";
$('#entities').append(entitiesData);
});
//alert('success');
// write the success status
$('#statusField'+uniqueId).addClass('alert alert-success pull-left');
$('#statusField'+uniqueId).append('Success!');
},
// to execute when error
error: function(jqXHR, textStatus, errorThrown){
//alert("error");
//alert(textStatus);
//alert(errorThrown);
// print the error
// write the error message
$('#statusField'+uniqueId).addClass('alert alert-error pull-left');
$('#statusField'+uniqueId).append('Error: '+textStatus+', '+errorThrown);
},
// always executed at last whether success or error
complete: function(){
// bring back submit button to its original state
$('.showinfo').button('reset');
// hide the progress bar
$('#progress').hide();
// fade in the results
$('#resultbox').fadeIn('slow', function(){
});
}
});
帮助!