为什么$就呼吁JSON数据触发错误回调时,HTTP状态code是" 200确定] ;? [英] Why does $.ajax call for json data trigger the error callback when http status code is "200 OK"?

查看:232
本文介绍了为什么$就呼吁JSON数据触发错误回调时,HTTP状态code是" 200确定] ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的Ajax请求:

I have the following ajax request:

		jQuery.ajax({
			async: true,
			type: "GET",
			url: url,
			data: data,
			dataType: "json",
			success: function(results){
				currentData = results;
			},
			error: function(xhr, ajaxOptions, thrownError){
				if (xhr.status == 200) {
					console.debug("Error code 200");
				}
				else {
					currentData = {};
					displayAjaxError(xhr.status);
				}
			}
		});

由于某种原因,错误回调被称为事件虽然HTTP状态code是200 IE浏览器。该请求确定。这是为什么?

For some reason the error callback is called event though the http status code is 200 ie. the request is OK. Why is this?

推荐答案

该问题可能是返回的JSON数据从URL的格式不正确。当服务器实际上返回的东西,HTTP状态code是200。但是,这并不意味着数据是正确的JSON。检查字符串化的JSON数据返回的正确形成。

The problem might be that the json data returned from the url is malformed. When the server actually returns something, the http status code is 200. But that doesn't mean that the data is proper json. Check that the stringified json data returned is correctly formed.

我回答我自己的guestion,因为我这学到了艰辛的道路。我没有在我的JSON数据躲过了-quote的性格。这导致非常奇怪的行为。幸运的是,双引号字符是pretty的多,需要从通过​​JSON传递数据逃脱的唯一角色。(更多这个问题:<一href="http://stackoverflow.com/questions/983451/where-can-i-find-a-list-of-escape-characters-required-for-my-json-ajax-return-typ">http://stackoverflow.com/questions/983451/where-can-i-find-a-list-of-escape-characters-required-for-my-json-ajax-return-typ)

I'm answering my own guestion because I learned this the hard way. I hadn't escaped a "-quote character in my json data. This resulted in very odd behaviour. Luckily the double quote character is pretty much the only character that needs to be escaped from data delivered via JSON. (More on this issue: http://stackoverflow.com/questions/983451/where-can-i-find-a-list-of-escape-characters-required-for-my-json-ajax-return-typ)

这篇关于为什么$就呼吁JSON数据触发错误回调时,HTTP状态code是&QUOT; 200确定] ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆