[英]Error: Uncaught SyntaxError: Unexpected token &
I get an error when sending JSON data to JavaScript from the models. 从模型向JavaScript发送JSON数据时出错。 It looks like encoding is causing the error, but all the examples I have found work for other people.
看起来编码导致错误,但我找到的所有示例都适用于其他人。 How can I properly send model data from my view to JavaScript?
如何正确地将模型数据从我的视图发送到JavaScript?
view code: 查看代码:
def home(request):
import json
info_obj = Info.objects.all()
json_data = serializers.serialize("json", info_obj)
return render_to_response("pique/home.html", {'json_data':json_data}, context_instance=RequestContext(request))
JavaScript code: JavaScript代码:
var data = jQuery.parseJSON('{{json_data}}');
console.log(data);
The error Uncaught SyntaxError: Unexpected token &
: 错误
Uncaught SyntaxError: Unexpected token &
:
var data = jQuery.parseJSON('[{"pk": 1, "model": "pique.eat" ...
You must use "
instead of "
in the string. 您必须在字符串中使用
"
而不是"
。
The string was automatically escaped by render_to_response
. 该字符串由
render_to_response
自动转义。
To avoid this you must mark json_data
safe. 为避免这种情况,您必须将
json_data
标记json_data
安全。 Use mark_safe
for it. 使用
mark_safe
。
from django.utils.safestring import mark_safe
return render_to_response(
"pique/home.html",
{
'json_data':mark_safe(json_data)
},
context_instance=RequestContext(request))
Your data is html encoded. 您的数据是html编码的。 It should come from the server with quotes and all.
它应该来自带引号的所有服务器。 Is
render_to_response
doing some sort of encoding? render_to_response
是否正在进行某种编码? What does json_data
look like before that function? 在该函数之前
json_data
什么样的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.