jquery ajax成功中的location.href没有触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21845662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:42:54  来源:igfitidea点击:

location.href in jquery ajax success is not triggered

jqueryajax

提问by Prashere

I am using jquery's $.ajax to send json data as post request to my internal server. I am able to get response, but on success function, while console.log works but not the immediate line location.href='/users'; even if I have location.href alone in the success, it isn't been triggered. Below is my code sample

我正在使用 jquery 的 $.ajax 将 json 数据作为 post 请求发送到我的内部服务器。我能够得到响应,但在成功功能上,console.log 工作但不是直接行 location.href='/users'; 即使我只有 location.href 成功,它也不会被触发。下面是我的代码示例

<script type='text/javascript'>
       function submitData() {
           var payload = {
               'fullname': $('#fullname').val(),
               'username': $('#username').val(),
               'email': $('#email').val(),
               'password': $('#password').val(),
           };

           var onSuccess = function (data, textStatus, jqXHR) {
               console.log(jqXHR.status);
               location.href= "/thankyou.html";
           };


           $.ajax({
                type: 'POST',
                async: false,
                url: '/users',
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify(payload),
                success: onSuccess,
            });
        }
</script>

I have also tried window.location, window.location.href, window.location.assign(), none seems to work in the above code. But strangely, when I define a complete callback for ajax and alert something, location.href on success works rarely (but not everytime). I have also looked through similar questions and tried those answers, but doesn't seems to fix this. Having async=true, gets an Broken Pipe error on my flask server.

我也试过 window.location、window.location.href、window.location.assign(),上面的代码似乎都不起作用。但奇怪的是,当我为 ajax 定义一个完整的回调并发出警报时,成功的 location.href 很少(但不是每次)。我也查看了类似的问题并尝试了这些答案,但似乎没有解决这个问题。使用 async=true 时,我的 Flask 服务器上会出现 Broken Pipe 错误。

采纳答案by Vivek Parekh

Try to call it this way:

尝试这样称呼它:

$.ajax({
            type: 'POST',
            async: false,
            url: '/users',
            contentType: 'application/json',
            dataType: 'json',
            data: JSON.stringify(payload),
            success: function(data, textStatus, jqXHR){
                console.log(jqXHR.status);
                window.location.href= "/thankyou.html";
            }
        });

Also your code for the ajax call has got a ,at success:event which is an error.

此外,您的 ajax 调用代码有一个,atsuccess:事件,这是一个错误。

回答by MrGorki

Try this:

尝试这个:

  $.ajax({
        type: 'POST',
        url: '/users',
        data: JSON.stringify(payload),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        async: false,
        cache: false,
        timeout: 100000,
        success: function (response) {
            location.href= "/thankyou.html";
        }
    });

回答by loki

Remove onSuccess function and put redirect inside success itself. OR check if u have any errors using firebug console so that your success is not triggered at all.

删除 onSuccess 函数并将重定向放入成功本身。或者使用萤火虫控制台检查您是否有任何错误,以便根本不会触发您的成功。