Javascript jQuery$.ajax在IE8中不起作用,但在FireFox&;铬
我有以下ajax调用,它在Firefox和Chrome中工作得非常好,但在IE中却不行:Javascript jQuery$.ajax在IE8中不起作用,但在FireFox&;铬,javascript,jquery,ajax,json,internet-explorer,Javascript,Jquery,Ajax,Json,Internet Explorer,我有以下ajax调用,它在Firefox和Chrome中工作得非常好,但在IE中却不行: function getAJAXdates( startDate, numberOfNights, opts ) { var month = startDate.getMonth() + 1; var day = startDate.getDate(); var year = startDate.getFullYear(); var d
function getAJAXdates( startDate, numberOfNights, opts ) {
var month = startDate.getMonth() + 1;
var day = startDate.getDate();
var year = startDate.getFullYear();
var d = new Date();
var randNum = Math.floor(Math.random()*100000000);
$.ajax({
type : "GET",
dataType : "json",
url : "/availability/ajax/bookings?rand="+randNum,
cache : false,
data : 'month='+month+'&day='+day+'&year='+year+'&nights='+numberOfNights,
contentType : 'application/json; charset=utf8',
success : function(data) {
console.log('@data: '+data);
insertCellData(data, opts, startDate);
},
error:function(xhr, status, errorThrown) {
console.log('@Error: '+errorThrown);
console.log('@Status: '+status);
console.log('@Status Text: '+xhr.statusText);
}
});
}
我知道所有变量都传递了正确的内容,$.ajax确实传递了所有参数/值
这是我在错误中得到的结果:
日志:@错误:未定义
日志:@状态:parsererror
日志:@状态文本:确定
我知道IE上的缓存问题,并实现了一个随机参数来清除它
这是我得到的JSON(我可以通过Charles看到)
最后,这些是从后端发送回的头文件:
header('Content-Type: application/json; charset=utf8');
header("Cache-Control: no-cache");
header("Expires: 0");
header('Access-Control-Max-Age: 3628800');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
有什么想法吗?大多数情况下,IE特定的解析错误都是由额外的逗号引起的。例如,
[1,2,3,]
在FF中有效,但在IE中无效。无论如何,您应该粘贴在JSON响应中,否则无法判断问题。我将注释掉contentType并添加数据类型:“JSON”
从
数据类型:您期望从服务器返回的数据类型
contentType:向服务器发送数据时,请使用此内容类型
您正在指定您正在发送json,但您没有-可能这就是问题所在?检查您的页面是否只返回
OK
或是否返回'OK'
。只有'OK'
是有效的JSON。使用类似于检查来自请求的值的工具。如果您只需键入警报(数据),会发生什么代码>或var myObject=eval(“(“+data+”)”)代码>
如果您通过在url栏上键入url直接从浏览器调用函数,那么您的ajax调用中包含“get”中的所有参数(¶m1=param1value¶m2=…)?您应该能够阅读响应
JSON响应中的某些东西让IE疯狂。您使用的是什么版本的jQuery
如果检查jquery的代码,则在调用jquery.httpData()时会抛出parsererror。以下是jquery中的代码:
if ( status === "success" ) {
// Watch for, and catch, XML document parse errors
try {
// process the data (runs the xml through httpData regardless of callback)
data = jQuery.httpData( xhr, s.dataType, s );
} catch(err) {
status = "parsererror";
errMsg = err;
}
}
也许jQuery.httpData()值得一看。也就是说,您可以检查jQuery.parseJSON是否被调用,以及它是否确实返回了一个对象
if ( typeof data === "string" ) {
// Get the JavaScript object, if JSON is used.
if ( type === "json" || !type && ct.indexOf("json") >= 0 ) {
console.log(data); //add this
data = jQuery.parseJSON( data );
console.log("data parsed successfully"); //add this
我在$.ajax()中也遇到了类似的问题(jqueryv1.4.2)。它在IE8中不起作用,而在Firefox中起作用
然而,我从IE8调试工具栏上注意到,我的页面处于怪癖模式。因此,我通过插入此doctype
强制使其在标准模式下工作。突然,$.ajax()开始工作了
我真的不太理解怪癖/标准模式,但“标准”这个词在某种程度上更接近于Firefox或Chrome,而不是IE。所以我就是这么想的
@请参见我添加了contentType以查看它是否解决了问题,但即使没有解决问题,它也无法工作。感谢JSON对象似乎还可以。我把它添加到我的原始问题中,这样你可以看一看。JSON通过了JSONLint测试。我得到一个“HTTP/1.1200 OK”回复
if ( typeof data === "string" ) {
// Get the JavaScript object, if JSON is used.
if ( type === "json" || !type && ct.indexOf("json") >= 0 ) {
console.log(data); //add this
data = jQuery.parseJSON( data );
console.log("data parsed successfully"); //add this