【发布时间】:2023-03-23 07:37:01
【问题描述】:
我已经按照 How do I JSON serialize a Python dictionary? 和 What is the correct JSON content type? 创建了这样的 json 响应:
def json_response(something):
return HttpResponse(
simplejson.dumps(something),
content_type = 'application/json; charset=utf8'
)
我有两个抱怨:
a) 当我直接在 Google Chrome 中点击我的 ajax URL 时,Inspector 说
资源解释为文档但 以 MIME 类型传输 应用程序/json。
也试过火狐,它可以作为文件下载。
b) 当我使用 jQuery.ajax 时,仅当我不传递 dataType 参数时才有效,如果我输入例如 dataType:'json' 那么我会在 jquery.js 文件中收到此错误
Uncaught SyntaxError: Unexpected token :
我做错了什么?
我正在使用 jquery 1.5.2 和 Django Trunk(r15915 大约两周前更新)并且只调用相同的域 url。
编辑: jQuery.getJSON 不起作用,但适用于 jquery 1.4.2。 在 Request Headers 上查看 firebug 控制台显示 Content-Type: application/x-www-form-urlencode,这可能与问题有关。
提前致谢
【问题讨论】:
标签: jquery ajax django json simplejson