Javascript Safari和iOS设备不支持jQuery AJAX调用

Javascript Safari和iOS设备不支持jQuery AJAX调用,javascript,jquery,ios,ajax,Javascript,Jquery,Ios,Ajax,我正在尝试通过AJAX设置cookie。它适用于Android设备、Chrome、FF、Opera、Microsoft Edge、IE,Safari和iOS设备除外。关于Safari和iOS,AJAX是否隐藏了一些神奇的东西 下面是我在Android设备上使用的代码 Javascript文件 function setCookie() { var $cookieName = 'example-cookie', $cookieMethod = 'example-metho

我正在尝试通过AJAX设置cookie。它适用于Android设备、Chrome、FF、Opera、Microsoft Edge、IE,Safari和iOS设备除外。关于Safari和iOS,AJAX是否隐藏了一些神奇的东西

下面是我在Android设备上使用的代码

Javascript文件

function setCookie() {
var $cookieName   = 'example-cookie',
            $cookieMethod = 'example-method',
            $cookieGet    = 'example-get'

        $.ajax({
            type: 'POST',
            url: $(location).attr('protocol') + 'to/location.php',
            data: {
                name: $cookieName,
                method: $cookieMethod,
                get: $cookieGet
            },
            cache: false,
            success: function(data, textStatus, jQxhr) {
                try {
                    $('.report .display-result').css('display', 'none').css('position', 'absolute')
                        .css('width', '100%').html($(data).filter('.display').html()).fadeIn(1000);

                    setTimeout(function() {
                        $('.report .display-result').fadeOut(1000);
                    }, 4000);
                } catch (err) {/*alert(err);*/}
            },
            error: function(jqXhr, textStatus, errorThrown) {
                try {
                    alert(jqXhr.responseText);
                } catch (err) {/*alert.log(err);*/}
            }
        });
    }
    <?php

require_once '../to/class/Class.php';

$uc           = new Class();
$cookieName   = $_POST['name'];
$cookieMethod = $_POST['method'];
$cookieId     = $_POST['get'];

$uc->method($_POST['name'], $_POST['method'], $_POST['get']);
?>

<?php
if (!empty($uc->method($_POST['name'], 'get-cookie'))):
    $cookie = json_decode($uc->method($_POST['name'], 'get-cookie'));

    // Making sure the cookie exists and is not empty.
    if (!empty($cookie)):
        // Making sure the cookie has id's, if not, will spit out 'Successly added to report'.
        if (isset($cookie->data->id)) {
            foreach ($cookie->data->id as $chkDuplicates) {
                // If any id's match, there is a duplicate. Make sure that they know they've added this already.
                if ($chkDuplicates == $_POST['get']) {
                    $duplicateFound = true;
                    break;
                }
            }
        }

        if (isset($duplicateFound)): ?>
            <div class="display info">
                <h3 class="alert alert-info">You've already added this to your report.</h3>
            </div>
        <?php else: ?>
            <div class="display success">
                <h3 class="alert alert-success">Successfully added to report.</h3>
            </div>
        <?php endif; ?>
    <?php endif; ?>
<?php else: ?>
    <div class="display failure">
        <h3 class="alert alert-failure">Unfortunately, that item wasn't added, please refresh the page and try again.</h3>
    </div>
<?php endif; ?>
PHP文件

function setCookie() {
var $cookieName   = 'example-cookie',
            $cookieMethod = 'example-method',
            $cookieGet    = 'example-get'

        $.ajax({
            type: 'POST',
            url: $(location).attr('protocol') + 'to/location.php',
            data: {
                name: $cookieName,
                method: $cookieMethod,
                get: $cookieGet
            },
            cache: false,
            success: function(data, textStatus, jQxhr) {
                try {
                    $('.report .display-result').css('display', 'none').css('position', 'absolute')
                        .css('width', '100%').html($(data).filter('.display').html()).fadeIn(1000);

                    setTimeout(function() {
                        $('.report .display-result').fadeOut(1000);
                    }, 4000);
                } catch (err) {/*alert(err);*/}
            },
            error: function(jqXhr, textStatus, errorThrown) {
                try {
                    alert(jqXhr.responseText);
                } catch (err) {/*alert.log(err);*/}
            }
        });
    }
    <?php

require_once '../to/class/Class.php';

$uc           = new Class();
$cookieName   = $_POST['name'];
$cookieMethod = $_POST['method'];
$cookieId     = $_POST['get'];

$uc->method($_POST['name'], $_POST['method'], $_POST['get']);
?>

<?php
if (!empty($uc->method($_POST['name'], 'get-cookie'))):
    $cookie = json_decode($uc->method($_POST['name'], 'get-cookie'));

    // Making sure the cookie exists and is not empty.
    if (!empty($cookie)):
        // Making sure the cookie has id's, if not, will spit out 'Successly added to report'.
        if (isset($cookie->data->id)) {
            foreach ($cookie->data->id as $chkDuplicates) {
                // If any id's match, there is a duplicate. Make sure that they know they've added this already.
                if ($chkDuplicates == $_POST['get']) {
                    $duplicateFound = true;
                    break;
                }
            }
        }

        if (isset($duplicateFound)): ?>
            <div class="display info">
                <h3 class="alert alert-info">You've already added this to your report.</h3>
            </div>
        <?php else: ?>
            <div class="display success">
                <h3 class="alert alert-success">Successfully added to report.</h3>
            </div>
        <?php endif; ?>
    <?php endif; ?>
<?php else: ?>
    <div class="display failure">
        <h3 class="alert alert-failure">Unfortunately, that item wasn't added, please refresh the page and try again.</h3>
    </div>
<?php endif; ?>

已成功添加到报表。
很遗憾,未添加该项目,请刷新页面并重试。

无论出于何种原因,这似乎不适用于Safari和iOS设备。每当我尝试获取警报(jqXhr)时,我什么也得不到。jqXhr.responseText也是一样,textStatus给我“error”,errorThrown和其他内容一样是空的。这件事我有点不知所措。非常感谢您的支持,并感谢您抽出时间来了解这一点。

Safari可以对AJAX请求进行大量缓存。也许您之前在Safari中测试了jQuery,然后响应完全正常,API给出了一个空响应

通过对API url
+'&nocache='+new Date().getTime()
进行以下更改,尝试使Safari缓存无效。这基本上是让Safari在每个请求上都像对待不同的url一样对待Ajax url,因此Safari将从API端点获得新的响应

 $.ajax({
            type: 'POST',
            url: $(location).attr('protocol') + 'to/location.php' + '&nocache=' + new Date().getTime(),
            data: {
                name: $cookieName,
                method: $cookieMethod,
                get: $cookieGet
            },
            cache: false,
            success: function(data, textStatus, jQxhr) {
                try {
                    $('.report .display-result').css('display', 'none').css('position', 'absolute')
                        .css('width', '100%').html($(data).filter('.display').html()).fadeIn(1000);

                    setTimeout(function() {
                        $('.report .display-result').fadeOut(1000);
                    }, 4000);
                } catch (err) {/*alert(err);*/}
            },
            error: function(jqXhr, textStatus, errorThrown) {
                try {
                    alert(jqXhr.responseText);
                } catch (err) {/*alert.log(err);*/}
            }
        });

好的,这与JS文件和AJAX的url有关

显然,iOS/Safari不读取

$(location).attr('protocol')
所以一定要把它改成

$(location).attr('origin')

嘘,这么简单的事情花了太长时间才找到。希望这对将来的任何人都有帮助。

这一点运气不好。我见过狩猎旅行会这样做。我想cache:false也可以解决这个问题。谢谢你的回复。