我正在尝试通过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文件
<?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给我'错误',而errorThrown与其他东西一样空白。我在这一点上有点亏。非常感谢任何支持,感谢您抽出时间来了解这一点。
答案 0 :(得分:0)
Safari可以对AJAX请求进行大量缓存。也许你之前在Safari中测试了你的jQuery,之后响应完全正常并且你的API给出了空响应。
尝试按照以下对API网址+ '&nocache=' + new Date().getTime()
的更改来使Safari缓存无效。这样做基本上使得Safari在每次请求时都将Ajax URL视为不同的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);*/}
}
});
答案 1 :(得分:0)
好吧,所以它与JS文件和AJAX的url有关。
显然,iOS / Safari无法阅读
$(location).attr('protocol')
因此请务必将其更改为
$(location).attr('origin')
Sheesh,这么简单的事情花了太长时间才能追踪到。希望这对未来的任何人都有帮助。