jQuery $ .ajax在IE8中不起作用,但在FireFox和Chrome上有效

发布于 2021-01-31 17:43:16

我有以下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);
        }
    });
}

我知道一个事实,所有变量都传递正确的内容,而$ .ajax确实传递所有参数/值。

这就是我遇到的错误:

日志:@错误:未定义日志:@状态:parsererror日志:@状态文本:确定

我知道IE上的缓存问题,并实施了一个随机参数来清除它。

这是我得到的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"
      }
   ]
}

最后,这些是从后端发送回的标头:

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');

有任何想法吗?

关注者
0
被浏览
106
1 个回答
  • 面试哥
    面试哥 2021-01-31
    为面试而生,有面试问题,就找面试哥。

    我会注释掉contentType并添加dataType:“ json”

    来自http://api.jquery.com/jQuery.ajax/

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

    contentType: 将数据发送到服务器时,请使用此内容类型。

    您指定要发送json,但不是-可能是问题所在?



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看