【发布时间】:2015-03-19 14:48:16
【问题描述】:
我不确定是否有人已经问过这个问题,至少我找不到答案,所以我想知道我的脚本做错了什么。
我正在尝试使用由 Jquery 完成的回调删除一个 cookie,该回调在后台调用一个 php 脚本,但是,无论我尝试什么,我都无法使其正常工作(删除 cookie)。
我检查了 php 网站,甚至查看了 RFC 2109 备忘录,以了解浏览器和 php 如何尝试完成删除 cookie。
所以,我的问题是;这怎么会失败?
编辑:我没有收到任何错误消息,手动删除 cookie 并使用 jquery 完成的相同类型的回调创建 cookie 也可以。当我尝试使用 jquery 进行相同的回调以在后台运行 PHP 脚本以删除 cookie 时,它不会被删除。
该 JQ 回调操作的代码:
$(document).ready(function() {
var alert = $('#Cmessage');
$(".delete").on('click', function(){
$.ajax({
url: 'http://www.oostpijl.nl/shop/offerte/deletecookie.php',
type: 'get', // form submit method get/post
dataType: 'json', // request type html/json/xml
beforeSend: function() {
alert.fadeOut();
},
success: function(result) {
if(result.error){
alert.html(result.html).fadeIn();
console.log(e)
}else{
alert.html(result.html).fadeIn();
}
}
});
});
});
PHP 脚本:
<?php
include("_offertesettings.php");
if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ){
setcookie('offerte', '', time() - 3600, '/' , '.oostpijl.nl' );
setcookie('offerteC', '', time() - 3600, '/', '.oostpijl.nl' );
$result = array("error" => false, "html" => null);
$result["error"] = false;
$result["html"] = "<script type='text/javascript'>setTimeout(function() { window.location='" . $config["BURL"] . "'; }, 10);</script>";
} else {
$result["error"] = true;
$result["html"] = "<h3>Error; Neem contact op met de webmaster</h3>";
}
echo json_encode($result);
exit;
?>
【问题讨论】:
-
尝试手动删除 cookie。然后创建一个新的,看看你是否仍然得到同样的错误。
-
您没有正确阅读我的问题。我没有收到任何错误代码,php 脚本运行并完成,就像没有任何问题一样。
-
Cookies 由浏览器管理。 PHP 通过请求浏览器 操作 cookie 以作用于 HTTP 响应标头。在您的情况下,浏览器是否真的读取了用于 cookie 操作的 AJAX 调用的响应标头?
-
还有……我怎么知道它是否读懂了?
-
是的,不知何故,我认为它确实读取了它,因为 cookie 的设置方式与我想删除 cookie 的方式相同,所以这就是为什么我觉得它没有被删除很奇怪。