我不确定是否有人已经问过这个问题,至少我找不到答案,所以我想知道我的脚本做错了什么。
我正试图通过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;
?>
确保进入isset条件。你应该通过打印一些东西作为测试来测试它,然后退出。
if( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ){
echo json_encode('test');
exit;
}
还要确保包含的文件"_offertesettings.php"中没有错误。您可以通过在php脚本顶部添加这些行来检查是否存在php错误。
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
要删除你的cookie,你只需要这个:
setcookie('offerte', '', time() - 3600);
setcookie('offerteC', '', time() - 3600);