简体   繁体   English

JSON.parse()无法正常工作

[英]JSON.parse() is not working

in my server (python/django) side, 在我的服务器(python / django)端,

I have an array like this: 我有一个像这样的数组:

comments = [['rwerw', '215', '/news/215/'], ['wrwerwer', '215', '/news/215/'], ['Woinfoqf', '215', '/news/215/'], ['Good', '215', '/news/215/'], ["He's good", '215', '/news/215/']]

which I pass through to my ajax call as a string: 我将它作为字符串传递给我的ajax调用:

...

return JsonResponse({'comments': str(comments)})

I then use JSON.parse() to turn it back into an array object. 然后,我使用JSON.parse()将其转换回数组对象。

console.log(data.comments); // logs the json
var comments = JSON.parse(data.comments); // error 

However for some reason JSON.parse() is not working. 但是由于某种原因, JSON.parse()无法正常工作。 When I use it I get the following error: 使用它时,出现以下错误:

Error: 错误:

SyntaxError: JSON.parse: unexpected character at line 1 column 3 of the JSON data
.success 
n.Callbacks/i 
n.Callbacks/j.fireWith 
z 
.send/c/<

Any idea what the problem is? 知道是什么问题吗?

您无需将注释列表转换为字符串-使其成为列表,并让JSONResponse正确地将整个字典转储为JSON:

return JsonResponse({'comments': comments})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM