admin管理员组文章数量:1425783
The Google App Engine server is returning a JSON String in response to a 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. The header is being set, and the jQuery code is parsing the response correctly.
However, in production, on the Google Frontend (when I deploy to production), the page is breaking because jQuery is not parsing the response correctly. 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".
So that probably means I'm trying to set an illegal/non-standard Content-Type. BUT, it works locally, and that is how I got my jQuery to understand the JSON String.
So, how do I format the response so that jQuery will know it's JSON and parse correctly?
The Google App Engine server is returning a JSON String in response to a 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. The header is being set, and the jQuery code is parsing the response correctly.
However, in production, on the Google Frontend (when I deploy to production), the page is breaking because jQuery is not parsing the response correctly. 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".
So that probably means I'm trying to set an illegal/non-standard Content-Type. BUT, it works locally, and that is how I got my jQuery to understand the JSON String.
So, how do I format the response so that jQuery will know it's JSON and parse correctly?
Share Improve this question edited May 14, 2011 at 13:10 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Feb 4, 2011 at 22:58 Will CurranWill Curran 7,13015 gold badges61 silver badges92 bronze badges 1- There are no "illegal content types" for responses from App Engine. How did you determine that it's not setting the correct content type? – Nick Johnson Commented Feb 6, 2011 at 23:40
3 Answers
Reset to default 4Just pass in "json"
as the dataType
argument to the AJAX call, and presto, the response will be treated as json no matter the content-type :)
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"
? It might be that your local server is forgiving about the parameters, while the Google servers will just submit the header as-is. Can you check the headers that are being returned via Firebug or WebKit developer panel, to see what response headers are being sent?
本文标签:
版权声明:本文标题:javascript - jQuery cannot understand JSON response when Content-Type is not set to "applicationjson" - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745451415a2658904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论