Jquery 当content type设置为application/JSON时,JSON.parse()不起作用
在尝试解析从服务器返回的JSON字符串时,我遇到了jquery(错误/错误):Jquery 当content type设置为application/JSON时,JSON.parse()不起作用,jquery,ajax,json,content-type,Jquery,Ajax,Json,Content Type,在尝试解析从服务器返回的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 我注意到,当标题内容类型未设置为“application/JSON”时,JSON
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
我注意到,当标题内容类型未设置为“application/JSON”时,JSON.parse可以正常工作,但如果内容类型设置为JSON,则无法正常工作。你知道为什么会这样吗
不工作代码:
// ...
success: function( response ) {
var responseData = $.parseJSON(response);
}
{"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));
控制器代码:
$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);
}
{"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));
标题
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
响应:
// ...
success: function( response ) {
var responseData = $.parseJSON(response);
}
{"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));
有效的代码
控制器:
// ...
success: function( response ) {
var responseData = $.parseJSON(response);
}
{"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与实际工作的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"}
如果内容类型设置为json并将响应作为json传递,则需要设置 数据类型:“json”
在您的ajax中。否则它将不起作用。我想这就是问题所在。如果它没有显示您的ajax。当您没有为
$调用指定数据类型
选项时。ajax
调用,jQuery将尝试根据响应中返回的内容类型
头解析响应
因此,当您没有为数据类型
指定任何内容,也没有为内容类型
标题指定任何特殊内容时,响应
被解析为文本
当您未为数据类型
指定任何内容,而为内容类型
标题指定“json”时,响应
将解析为json(Javascript对象文字)
当您将数据类型
指定为“json”时,内容类型
头是什么并不重要,响应
被解析为json(Javascript对象文本)
尝试将对象文本传递给JSON.parse将失败,因为它只接受字符串
因此,您需要确定您正在设置的内容和尝试调用的内容(JSON.parse
),并使用正确的组合。@Ian答案是完整的,但乍一看对我来说有些模糊
只是为了简化:如果您在请求(dataType:“JSON”
)和响应('Content-type','application/JSON'
)中都指定了JSON,那么响应已经是JSON对象,无需解析它。
而不是
var responseData=$.parseJSON(response)代码>
,
只是:
var responseData=response
不同的来源说,当jquery使用json时,使用text/javascript
。我知道这没有任何意义,但至少试一下。