jQuery-Can't让ajax请求在IE中工作,没有给出有用的错误消息

jQuery - Can't get ajax request to work in IE, no useful error messages given

本文关键字:有用 消息 错误 工作 ajax IE 请求 jQuery-Can      更新时间:2023-09-26

我在InternetExplorer中执行一个简单的.getJSON请求时遇到问题(下面代码的两种变体都适用于所有其他浏览器)。特别是下面的部分——我能够在代码的其他部分成功地执行.getJSON请求,但由于某种原因,下面的部分拒绝合作。

var dateFormatted = moment(date, "DD/MM/YYYY");  
var get_dates_url = window.location.href + "/available_dates?start="+dateFormatted._d+"&end="+dateFormatted._d+"&_="+(new Date().getTime());
console.log(get_dates_url);
$.getJSON(get_dates_url, function(result) {
  console.log(result);
  $("#appendList").html("Select a timeslot you'd like to schedule");
        var available_times = JSON.stringify(availableTimes(result));
        var service_length = $("#serviceMinutes").text();
        var get_times_url = window.location.href + "/available_times?available_times="+available_times+"&service_length="+service_length+"&_="+Math.random();
        console.log(get_times_url);
        $.ajax({
          url: get_times_url,
          cache: false,
          crossDomain: true,
          dataType: "JSON",
          method: 'GET',
          success: function(data){
            console.log(data);
            $.each(data, function() {
              var parseTime = moment.parseZone(this.time);
              var availableTime = parseTime.format('hh:mm a');
              $("#appendList").append("<li class='timeslot' data-time='" + this.time + "' data-formatted_time='" + availableTime + "'>" + availableTime  + " <button class='scheduler-button green-btn'>Select</button></li>");
            });
            selectDateButton();
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus); alert("Error: " + errorThrown);
          }
        });
});

生成的console.log(get_dates_url);看起来像这样:

"http://localhost:3000/professionals/29/available_dates?start=2015-11-05T00:00.000Z&end=2015-11-05T00:00.000Z&amp_=1446741826656"

根据其他建议,我已经尝试通过添加JSON.stringfy(new Date().getTime())来防止任何类型的缓存问题;在我的URL末尾。

我还添加了jQuery.support.cors=true;以防出现跨源问题。

我还将原始.getJSON代码修改为ajax请求,如下所示:

$.ajax({
      url: get_dates_url,
      cache: false,
      crossDomain: true,
      dataType: "json",
      success: function(result) {
        console.log(result);
        var available_times = JSON.stringify(availableTimes(result));
        var service_length = $("#serviceMinutes").text();
        var get_times_url = window.location.href + "/available_times?available_times="+available_times+"&service_length="+service_length+"&_="+Math.random();
        console.log(get_times_url);
        $.ajax({
          url: get_times_url,
          cache: false,
          crossDomain: true,
          dataType: "JSON",
          method: 'GET',
          success: function(data){
            console.log(data);
            $.each(data, function() {
              var parseTime = moment.parseZone(this.time);
              var availableTime = parseTime.format('hh:mm a');
              $("#appendList").append("<li class='timeslot' data-time='" + this.time + "' data-formatted_time='" + availableTime + "'>" + availableTime  + " <button class='scheduler-button green-btn'>Select</button></li>");
            });
            selectDateButton();
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus); alert("Error: " + errorThrown);
          }
        });
      },
      error: function (request, status, error) { alert(status + ", " + error); }
    });

我从IE中得到的只是一条错误消息,上面写着"错误",控制台中没有可见的错误。有什么想法可以引起这个问题吗?

编辑:

我也尝试过在我的URL末尾添加"&callback=?"。

哇,我希望这能帮助任何遇到同样问题的人,但事实证明IE特别不喜欢我通过这种方式传递数据来构建URL的方式。相反,通过ajax数据属性传递数据:

var dataToSend = { 'start' : dateFormatted._d, 'end' : dateFormatted._d };
$.ajax({
    url: window.location.href + "/available_dates",
    cache: false,
    data: dataToSend,
    dataType: "json",
    success: function(result) {
      console.log(result)
    }
});

我还添加了$.support.cors=true;到我的javascript文件。

我不完全确定是哪一个纠正了我的问题。