gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:36:36 25 4
gpt4 key购买 nike

Google App Engine 服务器正在返回 JSON 字符串以响应 POST:

self.response.headers.add_header("Content-Type", "application/json")       
return self.response.out.write(json_string)

这适用于在 Google App Engine 启动器中运行的本地开发服务器。正在设置 header ,并且 jQuery 代码正在正确解析响应。

但是,在生产中,在 Google 前端(当我部署到生产时),页面会损坏,因为 jQuery 无法正确解析响应。我可以发现本地和生产之间的唯一区别(它们运行相同的代码)是生产的响应忽略我将 Content-Type 设置为“application/json”的尝试。

所以这可能意味着我正在尝试设置非法/非标准的内容类型。但是,它在本地工作,这就是我如何让 jQuery 理解 JSON 字符串。

那么,如何格式化响应以便 jQuery 知道它是 JSON 并正确解析?

最佳答案

只需传入 "json" 作为 AJAX 调用的 dataType 参数,然后,无论内容类型如何,响应都将被视为 json :)

关于javascript - 当 Content-Type 未设置为 "application/json"时,jQuery 无法理解 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4903752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com