我正面临着一个奇怪的错误。该代码适用于firefox。
$.ajax({
url: someurl,
type: 'POST',
data: {},
headers: headers,
success: function(data) {
if (data.href) {
// create cookies
if (manager) {
window.location.href = "/index.html";
} else if (admin) {
window.location.href = "/admin.html";
} else {
window.location.href = "/tester.html";
}
}
},
error: function(data) {
$('#error').html("Invalid username or password.");
}
});
如果页面的url更改为someurl / index.html并且有大量的ajax调用,而href会立即更改,但在加载所有数据后页面将重定向到someurl / index.html。
答案 0 :(得分:2)
我发现了问题并制定了解决方案。
问题在于异步ajax调用。我在调用之前将异步设为false,并在调用后将其设置为true。这在每种情况下都有效。
async = false;
$.ajax({
url: someurl,
type: 'POST',
data: {},
async : false,
headers: headers,
success: function(data) {
if (data.href) {
// create cookies
if (manager) {
window.location.href = "/index.html";
} else if (admin) {
window.location.href = "/admin.html";
} else {
window.location.href = "/tester.html";
}
}
},
error: function(data) {
$('#error').html("Invalid username or password.");
}
});
async = true;
答案 1 :(得分:0)
像这样运行你的ajax调用
$(window).load(function() {
// ajax call
});