[英]jQuery cannot understand JSON response when Content-Type is not set to “application/json”
The Google App Engine server is returning a JSON String in response to a POST: Google App Engine服务器正在返回JSON字符串以响应POST:
self.response.headers.add_header("Content-Type", "application/json")
return self.response.out.write(json_string)
This works on my local development server running in the Google App Engine Launcher. 这适用于在Google App Engine启动器中运行的本地开发服务器上。 The header is being set, and the jQuery code is parsing the response correctly.
正在设置标头,并且jQuery代码正确解析了响应。
However, in production, on the Google Frontend (when I deploy to production), the page is breaking because jQuery is not parsing the response correctly. 但是,在生产环境中,在Google Frontend上(当我部署到生产环境时),页面中断了,因为jQuery无法正确解析响应。 The ONLY difference I can find between local and production (they are running the same code) is that the response on production, is ignoring my attempt to set the Content-Type to "application/json".
我可以发现本地和生产(它们运行相同的代码)之间的唯一区别是生产上的响应忽略了我将Content-Type设置为“ application / json”的尝试。
So that probably means I'm trying to set an illegal/non-standard Content-Type. 因此,这可能意味着我正在尝试设置非法/非标准的Content-Type。 BUT, it works locally, and that is how I got my jQuery to understand the JSON String.
但是,它在本地工作,这就是我让jQuery理解JSON String的方式。
So, how do I format the response so that jQuery will know it's JSON and parse correctly? 那么,如何格式化响应,以便jQuery知道它是JSON并正确解析?
只需将"json"
作为dataType
参数传递给AJAX调用即可,并且无论内容类型如何,响应都将被视为json :)
Try with: 试试:
self.response.headers['Content-Type'] = "application/json"
self.response.out.write(json_string)
This may seem silly, but maybe you need to capitalize the first parameter to add_header
, so it will be "Content-Type"
? 这可能看起来很愚蠢,但是您可能需要将第一个参数大写为
add_header
,因此它将是"Content-Type"
? It might be that your local server is forgiving about the parameters, while the Google servers will just submit the header as-is. 可能是您的本地服务器原谅了这些参数,而Google服务器只会按原样提交标头。 Can you check the headers that are being returned via Firebug or WebKit developer panel, to see what response headers are being sent?
您可以检查通过Firebug或WebKit开发人员面板返回的标头,以查看发送了哪些响应标头吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.