Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Баг с post запросом получающим json данные (jquery) (https://javascript.ru/forum/jquery/18161-bag-s-post-zaprosom-poluchayushhim-json-dannye-jquery.html)

Gri3li 20.06.2011 08:05

Баг с post запросом получающим json данные (jquery)
 
В jquery-1.5.2 и jquery-1.6.1 глючно работает post запрос получающий json данные, в jquery-1.3 все ок, но мне нужно не младше 1.5.


Вот клиентский код:

$(function () {

	$.ajax({
		url: 'example.php',
		type: 'POST',
		data: {data: ''},
		dataType: 'json',
		//dataType: 'text',
		success: function (r) {
			alert('success !!!');
		},
		error: function (r) {
			alert(
				'Ajax Error \n' +
				'\n readyState: ' + r.readyState + 
				'\n responseText: ' + r.responseText + 
				'\n status: ' + r.status + 
				'\n statusText: ' + r.statusText
			);
		}
	});	
	
});



Вот серверный код:

<?php
//header('Content-type: text/json; charset=utf-8'); // и так пробовал 
header('Content-type: application/json; charset=utf-8');
//header('Content-type: text/plain; charset=utf-8');

echo '{status: 1}';

?>



Вот что говорит alert:

Код:

Ajax Error

 readyState: 4
 responseText: {status: 1}
 status: 200
 statusText: parsererror


Вот что говорит FireBug:

Headers
Код:

Response Headers
Date: Mon, 20 Jun 2011 03:48:48 GMT
Server: Apache/2.2.4 (Win32) mod_ssl/2.2.4 OpenSSL/0.9.8d PHP/5.2.4
X-Powered-By: PHP/5.2.4
Content-Length: 11
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8

Request Headers
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.1) Gecko/20090624 Firefox/3.5
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: ru,en-us;q=0.7,en;q=0.3
Accept-Encoding        : gzip,deflate
Accept-Charset        : windows-1251,utf-8;q=0.7,*;q=0.7
Keep-Alive        : 300
Connection: keep-alive
Content-Type        : application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest

Response
Код:

{status: 1}
JSON
Код:

status: 1

В принципе можно в js коде dataType: 'json' поменять на dataType: 'text' и в php header('Content-type: application/json; charset=utf-8'); на header('Content-type: text/plain; charset=utf-8');
так все работает, а результат можно обрабатывать с помощью функции eval().

Но как то не кошерно, да и время дофига на поиск решения потратил, хочется нормально сделать.
Кто нибудь с этим сталкивался?

nikita.mmf 20.06.2011 10:39

JSON.parse('{status: 1}')//SyntaxError: Unexpected token ILLEGAL
JSON.parse('{"status": 1}')//Object

Gri3li 20.06.2011 11:35

Спасибо огромное :)


Часовой пояс GMT +3, время: 04:27.