Javascript jQuery $.ajax 不适用于 IE8,但适用于 FireFox 和 Chrome
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2721970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery $.ajax Not Working in IE8 but it works on FireFox & Chrome
提问by Sam3k
I have the following ajax call which works perfectly in Firefox and Chrome but not IE:
我有以下 ajax 调用,它在 Firefox 和 Chrome 中完美运行,但不适用于 IE:
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);
}
});
}
I know for a fact that all the variables are passing the right content and $.ajax is indeed passing all the paramater/values.
我知道所有变量都在传递正确的内容,而 $.ajax 确实在传递所有参数/值。
This is what I get on error:
这是我得到的错误:
LOG: @Error: undefined LOG: @Status: parsererror LOG: @Status Text: OK
日志:@Error:未定义日志:@Status:解析器错误日志:@Status 文本:OK
I'm aware of the cache issue on IE and implemented a random paramater to clear it up.
我知道 IE 上的缓存问题并实现了一个随机参数来清除它。
Here is the JSON i get back (i'm able to see it using Charles)
这是我返回的 JSON(我可以使用 Charles 看到它)
{
"availability":[
{
"inventory_id":"5",
"booking_id":"21",
"start_date":"05-01-2010",
"number_nights":4,
"text":"deFrancisco, Martin - 0.00 ACTIVE",
"type":"BOOKING"
}
]
}
Finally these are the headers that are sent back from the backend:
最后,这些是从后端发回的标头:
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');
Any ideas?
有任何想法吗?
采纳答案by house9
I would comment out the contentType and add dataType: "json"
我会注释掉 contentType 并添加 dataType: "json"
from http://api.jquery.com/jQuery.ajax/
来自http://api.jquery.com/jQuery.ajax/
dataType: The type of data that you're expecting back from the server.
dataType:您期望从服务器返回的数据类型。
contentType: When sending data to the server, use this content-type.
contentType:向服务器发送数据时,使用此内容类型。
you are specifying that you are sending json, but you are not - maybe this is the issue?
你指定你正在发送 json,但你不是 - 也许这就是问题所在?
回答by Tgr
Most of the time IE-specific parse errors are caused by extra commas. For example, [1, 2, 3,]is valid in FF but not in IE. Anyway, you should paste in the JSON response, it is impossible to tell the problem without that.
大多数情况下,特定于 IE 的解析错误是由额外的逗号引起的。例如,[1, 2, 3,]在 FF 中有效但在 IE 中无效。无论如何,您应该粘贴 JSON 响应,没有它就无法说明问题。
回答by Roland
I also have encountered a somewhat similar problem with $.ajax() (jquery v1.4.2). It's not working in IE8, whereas in Firefox it's working.
我也遇到了一个与 $.ajax() (jquery v1.4.2) 类似的问题。它在 IE8 中不起作用,而在 Firefox 中它起作用。
However, I've noticed from the IE8 debug toolbar that my page is in quirks mode. So, I forcefully make it to work in standard mode by inserting this doctype <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">. Suddenly the $.ajax() works!
但是,我从 IE8 调试工具栏中注意到我的页面处于 quirks 模式。因此,我通过插入此 doctype 强行使其在标准模式下工作<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">。突然间 $.ajax() 起作用了!
I don't really understand about quirks/standard mode but the word "standard" somehow feels closer to Firefox or Chrome, rather than IE. So that's how I got the idea.
我不太了解怪癖/标准模式,但“标准”这个词在某种程度上感觉更接近 Firefox 或 Chrome,而不是 IE。所以这就是我的想法。
回答by chiborg
回答by emas
What if you simply type alert(data);or var myObject = eval('(' + data + ')');?
如果您只输入alert(data);或var myObject = eval('(' + data + ')');怎么办?
And if you call the function directly from the browser by typing on the url bar your ajax call with all the parameters in "get" (¶m1=param1value¶m2=...)? You should be able to read the response.
如果您通过在 url 栏上键入您的 ajax 调用直接从浏览器调用该函数,并使用“get”中的所有参数(¶m1=param1value¶m2=...)?您应该能够阅读回复。
Something in JSON response is making IE crazy.
JSON 响应中的某些内容让 IE 发疯。
回答by devpl
What version of jQuery are you using?
您使用的是哪个版本的 jQuery?
If you check jquery's code, parsererror is thrown when jQuery.httpData() is called. Here's the code from jquery:
如果检查 jquery 的代码,调用 jQuery.httpData() 时会抛出解析器错误。这是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;
}
}
Maybe jQuery.httpData() is worth looking at. That is, you can check if jQuery.parseJSON is called and if it indeed returns an object.
也许 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

