我总是在使用cakephp获得json响应解析错误。 json语法是正确的 - 使用http://www.jslint.com/
进行验证responseText: {"success":1,"reload":0}
status: 200
响应标题:
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection Keep-Alive
Content-Length 30
Content-Type application/json; charset=UTF-8
Date Thu, 20 Mar 2014 10:46:24 GMT
Expires Mon, 26 Jul 1997 05:00:00 GMT
Keep-Alive timeout=5, max=100
Last-Modified Thu, 20 Mar 2014 10:46:24 GMT
Server Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.6
X-Powered-By PHP/5.5.6
请帮助我 - 我没有发现错误。
答案 0 :(得分:0)
你是否在创建json之前添加了内容标题
header('Content-Type:application / json');
答案 1 :(得分:0)
Cakephp应该这样做吗?!?
控制器: $ this-> autoRender = false; $这 - >请求 - > onlyAllow( 'AJAX'); $ response = array('success'=> 0); $ response ['reload'] = 0; .... return json_encode($ response);
jslint说JSON响应没问题:[code] {“success”:1,“reload”:0} [/ code]
jquery的:
$.ajax({
type: 'POST',
url: '<?php echo $this->Html->url(array('action' => 'sortfields')); ?>',
dataType: 'json',
data: JSON.stringify(mydata),
success: function(response) {
.....
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log('responseText: ' + jqXHR.responseText);
console.log(textStatus);
console.log(errorThrown);
}
})
firebug消息:
status=200 OK
responseText: {"success":1,"reload":0}
parsererror
SyntaxError: JSON.parse: unexpected character
return a.JSON.parse(b + "");