我正在实现Mozilla的个人身份验证(即browserid)。因此,我在Login()上有以下内容:
function onLogin(pAssertion) {
var sPHPSessionID = $.cookies.get("PHPSESSID")+'';
var sFoolCache = new Date().getTime() + '' + Math.random();
$.ajax(
{
cache: false,
type: 'POST',
url: '/webservice.php?id=personaauth&u=i',
data: { assertion: pAssertion, PHPSESSID: sPHPSessionID, z: sFoolCache },
success: function(res, status, xhr) { alert("do reload"); },
error: function(xhr, status, err) {
navigator.id.logout();
alert("Login failure: " + err);
}
});
}
在有"alert("do reload")"的地方,我通常会调用window.location.reload()。问题是,尽管我尽了一切努力清除并不使用浏览器缓存(Firefox),但我的/webservices.php页面根本没有被调用。ajax调用立即执行"success"函数(如果我在代码中留下"reload()"调用,它会使我的窗口重新加载保持在一个戏剧性的循环中)。我在/webservices.PHP调用中需要PHP会话id,所以我将PHPSSIONID cookie传递到页面。
我清空了我的萤火虫缓存。我关闭了浏览器,在url中添加了"sFoolCache"变量(而不是帖子),但它也不起作用。我迷路了。
看起来传递URL有问题,请尝试传递完整的URL:
function onLogin(pAssertion) {
var sPHPSessionID = $.cookies.get("PHPSESSID")+'';
var sFoolCache = new Date().getTime() + '' + Math.random();
$.ajax(
{
cache: false,
type: 'POST',
url: 'FullURL and querystring',
data: { assertion: pAssertion, PHPSESSID: sPHPSessionID, z: sFoolCache },
success: function(res, status, xhr) { alert("do reload"); },
error: function(xhr, status, err) {
navigator.id.logout();
alert("Login failure: " + err);
}
});
}
在这种情况下,它似乎有所帮助,我正在取消删除这个答案
查看此处:http://api.jquery.com/jquery.ajax/
弃用通知:jqXHR.success()、jqXHR.error()和从jQuery 1.8开始,jqXHR.complete()回调已被弃用。准备使用jqXHR.done()、jqXHR.fail(),而改为jqXHR.always()。
使用这个替代:
$.ajax(/* ... */)
.done(function(data) { /* ... */ })
.failure(function(data) { /* ... */ });
所以,感谢@Michal,以下是我如何重写它以使其工作的(顺便说一句,我必须自己回答,因为我的评论太长了):
function onLogin(pAssertion) {
var sPHPSessionID = $.cookies.get("PHPSESSID")+'';
var sFoolCache = new Date().getTime() + '' + Math.random();
$.ajax(
{
cache: false,
type: 'POST',
url: '/webservice.php?id=personaauth&u=i',
data: { assertion: pAssertion, PHPSESSID: sPHPSessionID, z: sFoolCache }
}).done(function(res, status, xhr) {
window.location.reload();
}).fail( function(xhr, status, err) {
navigator.id.logout();
alert("Login failure: " + err);
});
}
请投票给Michal的答案(在这里),而不是这个;-)谢谢大家。