jquery - 多个跨域 AJAX 调用。为什么 IE<9 会忘记 session ?

标签 jquery ajax json internet-explorer safari

我的页面包含 4 个 AJAX 请求,这些请求会触发,以便将订单请求发送到第三方系统。首先,发送产品信息,然后发送附加到订单的客户详细信息,然后发送适用于订单的任何注释,最后发送“完成”订单的第四个请求。

在 IE9、Firefox (mac + pc)、Safari (mac + pc) 和 Chrome (mac + pc) 中一切正常,但是,当从 IE<9 发送请求时,AJAX 请求会正确触发返回的响应没有错误,但似乎它是通过每个调用的不同 session 发送的,因此第 3 方系统将 4 个不同的请求识别为来自不同的 session 。

如果我通过 IE8 中的地址栏一一发送请求,一切都会按预期工作,并且顺序是捆绑在一起的,只有当通过 jQuery .ajax 发送时, session 才会被忘记。

我可以做些什么来强制这些浏览器维持 session 吗?

这是我的代码:

//function to add the product to the cart and cascade down to finalise the order
function addToCart(){
    var jsonurl = "xxxxx/additem?variationID="+$('input[name="variationID"]').val()+"&token="+APIKey+"&callback=?";
    $.ajax({
        url:jsonurl,
        type:'GET',
        dataType:'json',
        success:function(data){
            if (data.response == "success"){
                addLeadCustomer();
            } else {
                displayEnquiryError();
            }


        },
        error:function(data){
            displayEnquiryError();
        }
    })  
}


//function to add the lead customer and cascade down to finalise the order
function addLeadCustomer(){
    //add the lead customer to the order in J6
    jsonurl = "http://xxxxx/leadcustomer?token="+APIKey+"&details[FirstName]="+$('input[name="Name"]').val()+"&details[Email]="+$('input[name="Email"]').val()+"&details[HomePhone]="+$('input[name="Phone"]').val()+"&callback=?";
    $.ajax({
        url:jsonurl,
        type:'GET',
        dataType:'json',
        success:function(data){
            if (data.response.ID > 0){
                updateOrderAdditionalInfo();
            }else{
                displayEnquiryError();
            }
        },
        error:function(data){
            displayEnquiryError();
        }
    })
}

//function to update the order with the additional info and cascade down to finalise the order
function updateOrderAdditionalInfo(){
    //update the order with additional information
    jsonurl = "http://xxxxx/updateorder?token="+APIKey+"&details[Notes]="+$('input[name="EnquiryDate"]').val()+"\n\n"+$('select[name="NumberNights"]').val()+" nights\n\n"+$('select[name="NumberPeople"]').val()+" people\n\n"+$('textarea[name="Comments"]').val()+"&callback=?";
    $.ajax({
        url:jsonurl,
        type:'GET',
        dataType:'json',
        success:function(data){
            if (data.response == "success"){
                completeOrder();

            }else{
                displayEnquiryError();
            }
        },
        error:function(data){
            displayEnquiryError();
        }
    });
}

//function to complete the order
function completeOrder(){
    //complete the "order"
    jsonurl = "http://xxxxx/completeorder?token="+APIKey+"&callback=?";
    $.ajax({
        url:jsonurl,
        type:'GET',
        dataType:'json',
        success:function(data){
            if (data.response == "success"){
                $('.waiting').fadeOut(function(){
                    $('.enquirySuccess').fadeIn();
                    $('.cartItemsHolder').empty();
                    $('.cartItemsHolder').html('We have received your itinerary');
                })
            }
        },
        error:function(data){
            displayEnquiryError();  
        }
    });
}

$('#Form_enquiryForm').submit(function(){
    validateForm();
    if (failedValidation == 0){
        $(this).fadeOut(function(){
            $('.waiting').fadeIn();
            //add the package to the cart
            addToCart();
        });
    }
    return false;
});

更新:我看到一些帖子,给人的印象是这可能是由于 IE8 的缓存造成的。这导致我在 AJAX 调用中尝试cache:false,并向查询字符串添加随机数参数 (&cachebuster="+Math.random()),但都没有解决问题。

最佳答案

我发现这是因为 IE8 默认处理第 3 方 cookie。它阻止了他们。通过在 IE8 中调低安全设置,我能够获得所需的行为。看来我们必须重写 API 来传回一个标识符,每次我们都可以触发该标识符以将请求绑定(bind)在一起。

更新:Safari 默认情况下也拒绝第 3 方 cookie。在测试设置 cookie 之前,我已从 Safari 中的地址栏触发了 API。在我重置 Safari 并重新测试后,它表现出与 IE8 相同的行为。

更新2:我们实际上以不同的方式解决了这个问题。我们设置了一个 API 来设置 session 并关闭窗口。然后,使用 jQuery,我们检测浏览器。如果是 IE<=8 或 Safari,我们会启动新 API 的弹出窗口(这会设置 session 并立即关闭窗口)。我们现在可以在应用程序中使用 session 。我们还使用 jQuery 设置了一个 cookie,其过期时间与购物车中的 session 相同,让我们知道我们已经设置了 session 。

无论如何它都不完美..例如,如果 jQuery cookie 丢失,那么我们必须在购物车上作为新 session 启动。如果购物车 session 丢失,那么我们将在不知不觉中继续向购物车开火,而购物车不会记住我们,这使我们回到最初的问题......并且不要让我开始弹出窗口拦截器:)然而,这是一个快速修复,直到我们实现更强大的东西。

关于jquery - 多个跨域 AJAX 调用。为什么 IE<9 会忘记 session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12344042/

相关文章:

javascript - AJAX 返回按钮

javascript - Animationend 事件未在 :after element 上触发

javascript - 更新悬停/异步时的 HTML 标题属性

javascript - 使用 window.open(url ,"_self") 打开下载窗口,但不打开 GET 请求

javascript - 如何检查数组中是否存在事件目标值

c# - 将 List<T> 序列化/反序列化为 JSON

java - 如何从 HashMap<String,Object> 中的 gson 获取字符串,如下所示

javascript - ASP.NET Identity - 使用具有 JavaScript 功能的角色

javascript - 在 Javascript 中迭代 JSON 响应

ajax - 部分 View 中的 Asp.net MVC WebGrid 未通过 Ajax 更新