简体   繁体   English

当Content-Type未设置为“ application / json”时,jQuery无法理解JSON响应

[英]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.

相关问题 将响应标头“ Content-Type”设置为“ application / json”时,HTTP响应正文可以为空吗? - Can HTTP response body be empty when response header 'Content-Type' is set to 'application/json'? 应该什么时候Content-Type是application / json - When should Content-Type be application/json 无法发布内容类型:application / json从角度到导轨 - Cannot POST with content-type: application/json from angular to rails 使用 Fetch API javascript 将 Content-Type 设置为 application/Json - set Content-Type to application/Json using Fetch API javascript $ .ajax响应在内容类型:application / json中为json中的xml内容生成错误 - $.ajax response generates error in content-Type:application/json for xml content in json 使用 node.js 处理内容类型 application/json 的服务器响应 - Processing a server response of content-type application/json using node.js Fetch: Content-Type: application/json 在 post 方法中没有改变。 模式设置为 cors - Fetch: Content-Type: application/json not changing in post method. Mode set to cors 如何在$ resource AngularJS中将内容类型设置为DELETE方法的application / json - How can I set the content-type as application/json for DELETE method in $resource AngularJS 为什么 header 中的内容类型仍然是 text/plain 虽然我将其设置为 application/json - Why content-type in header remains text/plain though I set it as application/json 当没有提供Content-Type''application / Json“时,C#[WebMethod]没有命中 - C# [WebMethod] is not hitting when Content-Type ''application/Json" is not provided
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM