Я всегда получаю ошибку синтаксического анализа json с помощью cakephp. синтаксис 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
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
пожалуйста, помогите мне - я не нашел ошибку.
Cakephp должен сделать это?!?
Контроллер: $this-> autoRender = false; $this-> request-> 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 + "");
вы добавили заголовок содержимого до создания json
header ('Content-Type: application/json');