我在尝试解析json数据时遇到错误Uncaught SyntaxError:Unexpected Token 这是我的ajax代码(json2.js): $.ajax({ type: 'POST', url: '/best_choose/invoice/item_search.json/', data: jsonQuery, dataType: 'json', contentType: 'ap
这是我的ajax代码(json2.js):
$.ajax({
type: 'POST',
url: '/best_choose/invoice/item_search.json/',
data: jsonQuery,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function(data){
var parsed = JSON.parse(data);
//do stuff
}});
我的看法:
json_serializer = serializers.get_serializer('json')()
serialized_q = json_serializer.serialize(queryset, ensure_ascii=False)
return HttpResponse(
serialized_q, mimetype='application/json'
)
来自debug serialized_q是一个包含有效json u’valid_json’的unicode字符串
当您将dataType设置为json时,jQuery会为您解析数据.因此,您不需要通过JSON.parse,您可以将数据作为普通的Javascript对象引用.