jQuery美元。ajax在IE8中不起作用,但它适用于FireFox和Chrome。

时间:2022-10-07 16:17:27

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

LOG: @Error:未定义的日志:@Status: parsererror LOG: @Status Text: 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 - $500.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?

什么好主意吗?

6 个解决方案

#1


4  

I would comment out the contentType and add dataType: "json"

我将注释掉contentType并添加数据类型:“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.

数据类型:您期望从服务器返回的数据类型。

contentType: When sending data to the server, use this content-type.

内容类型:当向服务器发送数据时,请使用此内容类型。

you are specifying that you are sending json, but you are not - maybe this is the issue?

您指定要发送json,但您不是——也许这就是问题所在?

#2


1  

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.

大多数时间特定的解析错误是由额外的逗号引起的。例如,[1,2,3]在FF中有效,但在IE中无效。无论如何,您应该在JSON响应中粘贴,如果没有这个,就不可能知道问题。

#3


1  

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 。突然,$ . 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。这就是我的想法。

@see http://en.wikipedia.org/wiki/Quirks_mode

@see http://en.wikipedia.org/wiki/Quirks_mode

#4


0  

Check if your page just returns OK or if it returns 'OK'. Only 'OK' is valid JSON. Use a tool like JSONLint to check the value that comes from the request.

检查您的页面是否返回OK,或者返回“OK”。只有“OK”是有效的JSON。使用JSONLint这样的工具检查来自请求的值。

#5


0  

What if you simply type alert(data); or var myObject = eval('(' + data + ')'); ?

如果您只是键入alert(数据);或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" (&param1=param1value&param2=...)? You should be able to read the response.

如果你直接从浏览器调用这个函数,你的ajax调用会在“get”(和param1=param1value&param2=…)中输入你的ajax调用。您应该能够阅读响应。

Something in JSON response is making IE crazy.

JSON的响应让IE抓狂。

#6


0  

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()时抛出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;
  }
}

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

#1


4  

I would comment out the contentType and add dataType: "json"

我将注释掉contentType并添加数据类型:“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.

数据类型:您期望从服务器返回的数据类型。

contentType: When sending data to the server, use this content-type.

内容类型:当向服务器发送数据时,请使用此内容类型。

you are specifying that you are sending json, but you are not - maybe this is the issue?

您指定要发送json,但您不是——也许这就是问题所在?

#2


1  

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.

大多数时间特定的解析错误是由额外的逗号引起的。例如,[1,2,3]在FF中有效,但在IE中无效。无论如何,您应该在JSON响应中粘贴,如果没有这个,就不可能知道问题。

#3


1  

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 。突然,$ . 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。这就是我的想法。

@see http://en.wikipedia.org/wiki/Quirks_mode

@see http://en.wikipedia.org/wiki/Quirks_mode

#4


0  

Check if your page just returns OK or if it returns 'OK'. Only 'OK' is valid JSON. Use a tool like JSONLint to check the value that comes from the request.

检查您的页面是否返回OK,或者返回“OK”。只有“OK”是有效的JSON。使用JSONLint这样的工具检查来自请求的值。

#5


0  

What if you simply type alert(data); or var myObject = eval('(' + data + ')'); ?

如果您只是键入alert(数据);或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" (&param1=param1value&param2=...)? You should be able to read the response.

如果你直接从浏览器调用这个函数,你的ajax调用会在“get”(和param1=param1value&param2=…)中输入你的ajax调用。您应该能够阅读响应。

Something in JSON response is making IE crazy.

JSON的响应让IE抓狂。

#6


0  

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()时抛出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;
  }
}

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