【问题标题】:JSON.parse() does not work when content-type is set to application/json当 content-type 设置为 application/json 时 JSON.parse() 不起作用
【发布时间】:2013-04-03 04:02:44
【问题描述】:

我在尝试解析从服务器返回的 JSON 字符串时遇到了 jquery(错误/错误):

Timestamp: 10/04/2013 21:05:12
Error: SyntaxError: JSON.parse: unexpected character
Source File: http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
Line: 3

我注意到当标头 Content-type 未设置为“application/json”时 JSON.parse 工作正常,但如果 Content-type 设置为 json 则不起作用。知道为什么会发生这种情况吗?

代码无效:

控制器代码:

  $response = array('data' => array('msg' => 'Form did not validate'), 'response_handler_fn' => 'sign_up_response_handler_fn');
  $this->getResponse()->setHttpHeader('Content-type','application/json');
  return $this->renderText(json_encode($response));

Javascript:

// ...
success: function( response ) {
var responseData = $.parseJSON(response);
}

标题

Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection  Keep-Alive
Content-Length  92
Content-Type    application/json
Date    Wed, 10 Apr 2013 20:05:12 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive  timeout=15, max=100
Pragma  no-cache
Server  Apache
X-Powered-By    PHP/5.3.5

回应:

{"data":{"msg":"Form did not validate"},"response_handler_fn":"sign_up_response_handler_fn"}

有效的代码

控制器:

  $response = array('data' => array('msg' => 'Form did not validate'), 'response_handler_fn' => 'sign_up_response_handler_fn');
  return $this->renderText(json_encode($response));

Javascript 与正常工作的相同

标题:

Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection  Keep-Alive
Content-Length  92
Content-Type    text/html; charset=utf-8
Date    Wed, 10 Apr 2013 20:09:04 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive  timeout=15, max=100
Pragma  no-cache
Server  Apache
X-Powered-By    PHP/5.3.5

回复:

{"data":{"msg":"Form did not validate"},"response_handler_fn":"sign_up_response_handler_fn"}

【问题讨论】:

  • 当 jquery 使用 json 时,不同的来源说使用 text/javascript。我知道这没有任何意义,但至少试一试。

标签: jquery ajax json content-type


【解决方案1】:

如果 Content-type 设置为 json 并将响应作为 json 传递,那么您需要设置

dataType:"json"

在你的 ajax 中。否则它将无法工作。我猜它的问题。如果它也没有显示你的 ajax。

【讨论】:

    【解决方案2】:

    当您没有为$.ajax 调用指定dataType 选项时,jQuery 会尝试根据响应中返回的Content-Type 标头解析响应。

    因此,当您没有为dataType 指定任何内容,并且没有为Content-Type 标头指定任何特殊内容时,response 将被解析为文本。

    当您没有为dataType 指定任何内容,而为Content-Type 标头指定“json”时,response 被解析为 JSON(Javascript 对象文字)。

    当您将dataType 指定为“json”时,Content-Type 标头是什么并不重要,response 被解析为 JSON(Javascript 对象文字)。

    尝试将对象文字传递给JSON.parse 将失败,因为它只接受字符串。

    因此,您需要确定要设置的内容和尝试调用的内容 (JSON.parse),并使用正确的组合。

    【讨论】:

      【解决方案3】:

      @Ian 的答案是完整的,但乍一看对我来说有点晦涩难懂。
      只是为了简化:如果您在请求(dataType: "JSON")和响应('Content-type','application/json')中都指定了 JSON,那么响应已经是一个 JSON 对象,不需要解析它。

      而不是
      var responseData = $.parseJSON(response);
      , 只是:
      var responseData = response.

      【讨论】:

        猜你喜欢
        • 2020-12-13
        • 1970-01-01
        • 1970-01-01
        • 2014-10-06
        • 2022-01-01
        • 1970-01-01
        • 2012-05-22
        • 1970-01-01
        • 2018-03-11
        相关资源
        最近更新 更多