【问题标题】:href changes but the page is redirected after the page is fully loaded using jquery in chromehref 更改,但在页面完全加载后使用 chrome 中的 jquery 重定向页面
【发布时间】:2013-01-03 06:55:07
【问题描述】:

我在 CHROME 中遇到了一个奇怪的错误。该代码适用于 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。

【问题讨论】:

  • 当然它会在你使用window.location.href时重定向。你到底想达到什么目标?
  • 这将重定向,但在页面完全加载后从当前页面重定向。我希望它在页面加载之前重定向。这正是 chrome 中正在发生的事情。

标签: jquery google-chrome


【解决方案1】:

我发现了问题并制定了解决方案。

问题在于异步 ajax 调用。我在通话之前将异步设为假,并在通话后将其设置为真。这在每种情况下都有效。

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;

【讨论】:

    【解决方案2】:

    像这样运行你的 ajax 调用

    $(window).load(function() {
      // ajax call 
    });
    

    【讨论】:

    • 为什么你认为这会有所作为?
    • 不知道为什么,但这会使系统无响应。设置超时(函数(){},100);为我工作。无论如何,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-06-10
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多