Javascript 如何使用 jQuery 重定向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14591428/
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-24 17:18:48  来源:igfitidea点击:

How do I use jQuery to redirect?

javascriptjquery

提问by Robert

I am using a HTML form to submit to a jQuery validation which then sends information to a PHP processing page through ajax.

我正在使用 HTML 表单提交给 jQuery 验证,然后通过 ajax 将信息发送到 PHP 处理页面。

Everything works 100% apart from when everything comes back true I can't get the page to redirect to a success page.

除了一切恢复正常之外,一切都 100% 工作,我无法让页面重定向到成功页面。

The code I have is:

我的代码是:

$.post(url,{ username: value_login.val(), firstname: value_firstname.val(), lastname: value_lastname.val(), email: value_email.val(), password: value_password.val()} , function(data) {

    if(data == 'success'){

        window.location.href = "http://example.com/Registration/Success/";
    } else {
       $('#error').text('error');
    }

});

I'm thinking you can't redirect using the window.location.hreffunction.

我认为您无法使用该window.location.href功能进行重定向。

回答by Joseph Silber

You forgot the HTTP part:

你忘记了 HTTP 部分:

window.location.href = "http://example.com/Registration/Success/";

回答by Razan Paul

Via Jquery:

通过jQuery:

$(location).attr('href','http://example.com/Registration/Success/');

回答by Robert

I found out why this happening.

我发现了为什么会发生这种情况。

After looking at my settings on my wamp, i did not check http headers, since activated this, it now works.

在我的 wamp 上查看了我的设置后,我没有检查 http 标头,因为激活了它,它现在可以工作了。

Thank you everyone for trying to solve this. :)

感谢大家试图解决这个问题。:)

回答by Nikola Mitev

This is a shorthand Ajax function, which is equivalent to:

这是一个简写的 Ajax 函数,相当于:

$.ajax({  type: "POST",
           url: url,  
          data: { username: value_login.val(), firstname: value_firstname.val(), 
                  lastname: value_lastname.val(), email: value_email.val(),
                  password: value_password.val()
                },
          dataType: "json"
       success: success// -> call your func here  
      });

Hope This helps

希望这可以帮助