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
Add a ment  | 

3 Answers 3

Reset to default 4

Just 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?

本文标签: