ajax not working on ipad

2020-02-11 12:03发布

I have a form:

<form id="orderForm" onsubmit="return prepareOrder(this);" action='@ConfigurationManager.AppSettings["EpayLogonUrl"]' method="POST">
         <input type="hidden" name="Signed_Order_B64" value="">
         <input type="hidden" name="email" size="50" maxlength="50" value="@Model.Email">
         <input type="hidden" name="appendix" value="@Model.AppendixInfo">
         <button class="wiz_button" type="submit" disabled="disabled">
         <span><span id="buy_button_name">Buy</span></span></button>
</form>

and a function PrepareOrder

function prepareOrder(form) {
    var selectedPayWay = $('.pay_cont.selected').data('way');
    var result;
    $.ajax({
        type: 'POST',
        url: '/Pay/CreateOrder',
        data: { payWay: selectedPayWay },
        success: function (response) {
            if (response.IsSuccess) {
                switch (selectedPayWay) {
                    case payWay.Terminal:
                        showBookingInfo(response.BookingId, response.ExpiredDate);
                        result = false;
                        break;
                    case payWay.Epay:
                        $("input[type=hidden][name=Signed_Order_B64]").val(response.SignedString);
                        $("input[type=hidden][name=appendix]").val(response.AppendixString);
                        result = true;
                        break;
                }

            } else {
                toastr.options.timeOut = 10000;
                toastr.info(response.Message);
                result = false;
            }
        },
        error: function () {
            result = false;
        },
        async: false
    });

    return result;
}

The problem is that on a new ipad (Safari) CreateOrder action is not called. On the desktop browser, it works fine. There are no errors in console. I tried to add an alert after:

success: function (response) {

like that:

success: function (response) {
alert(response.IsSuccess)

and alert return me true. Why? if CreateOrder is not called. I also added logging to CreateOrder action and there are no output strings.

2条回答
聊天终结者
2楼-- · 2020-02-11 12:20

Looks like this iOS Safari issue reproduced only in some versions. For me iPad with iOS 6.0.1 has this redundant caching, but on 6.1.2 has no. As a workaround I'm using fake data to force Safari not to look into its cache:

$.ajax({
    type: 'POST',
    url: '/Pay/CreateOrder',
    data: { payWay: selectedPayWay, fakeDataToAvoidCache: new Date()},
    success: function (response) {
               //logic goes here
    });

Basicly, It's weird, that Safari caches POST request which must not to be cacheable, according to the specification.

查看更多
男人必须洒脱
3楼-- · 2020-02-11 12:28

Safari and/or Ipad support a very strong caching. I had the same problem in my application, too. Try adding following attributes to your controller (or even base controller):

[OutputCache(NoStore = true, Duration = 0)]
查看更多
登录 后发表回答