简体   繁体   English

jQuery AJAX后重定向问题

[英]Redirect issue after jQuery AJAX

I have the following code that should redirect to the index page of my website after the AJAX method completes. 我有以下代码,应在AJAX方法完成后重定向到我的网站的索引页。 It redirect however the page doesn't have any content loaded, it is just an empty page with a background. 它重定向,但是页面没有任何内容加载,它只是一个有背景的空白页面。

function login(){
    var uname = document.getElementById("UserName").value;
    var upass = document.getElementById("Password").value;

    var response = $.ajax({ type: "POST",
        url: "../../php_scripts/ajax.php",
        data: "call=login(" + uname + ", " + upass + ")",
        async: false
    }).responseText;

    window.location.replace("http://www.domainname.com");
}

What could cause this problem? 是什么导致此问题? could this be a server issue? 这可能是服务器问题吗?

[EDIT] [编辑]

Also if I put the redirect call above the AJAX everything loads correctly. 另外,如果我将重定向调用放在AJAX上方,则所有内容都会正确加载。

[EDIT 2] [编辑2]

If I use an asynchronous AJAX with callback method of any kind it causes exact same problem. 如果我将异步AJAX与任何类型的回调方法一起使用,则会引起完全相同的问题。 I'm beginning to think that this is a jQuery bug. 我开始认为这是一个jQuery错误。

You need to do the redirect on success of the ajax call. 您需要在ajax调用成功后进行重定向。

If your request is async: false then you should not use ajax.done(), ajax.fail() etc methods to register the callback methods, instead you need to pass the callback methods using success/error/complete options to the ajax call 如果您的请求是异步的:false,那么您不应使用ajax.done(),ajax.fail()等方法注册回调方法,而需要将使用成功/错误/完成选项的回调方法传递给ajax调用

$.ajax({
  async: false,
  success: function(){
        window.location.replace("http://www.domainname.com");
  },
  error: function(){
  },
  complete: function(){
        window.location.replace("http://www.domainname.com");
  }
})

(Posted on behalf of the OP). (代表OP发布)。

After hours of tinkering I found the culprit of the problem. 经过数小时的修补,我发现了问题的根源。 It had nothing to do with JavaScript or AJAX - in fact the error originated in the SQL database. 它与JavaScript或AJAX无关-实际上,该错误源自SQL数据库。 There was an incorrectly setup up relationship constraint which caused my PHP record update method to fail. 设置关系约束不正确,导致我的PHP记录更新方法失败。

This made the method return a null back to the calling method which then was saved into a variable and then later a member method was called on that variable. 这使得该方法将空值返回给调用方法,然后将其保存到变量中,然后在该变量上调用成员方法。 Calling a member method on a null type broke the PHP which is why nothing was displayed on my page. 在null类型上调用成员方法会破坏PHP,这就是为什么我的页面上什么都没有显示的原因。

Use complete callback to redirects to desired page. 使用complete回调重定向到所需页面。 After completing request then you'll be redirected to the page. 完成请求后,您将被重定向到页面。

Try this: 尝试这个:

function login(){
    // use jquery anyway if you reference to this
    var uname = $("#UserName").val();
    var upass = $("#Password").val();

    var response = $.ajax({ type: "POST",
        url: "../../php_scripts/ajax.php",
        data: "call=login(" + uname + ", " + upass + ")",
        async: false,
        complete: function() { 
            // redirects is done with below code
            window.location.replace("http://www.domainname.com");
        }
    }).responseText;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM