[英]why do we need to place the source name to redirect?
我在另一篇文章( jQuery Ajax PHP重定向到另一个页面 )中看到,当使用ajax重定向到PHP页面时,我们需要设置一个事件,如下所示:
$.ajax({
type: "POST",
url: "ajax.php",
data: dataString,
success: function(r)
{
window.location = 'new.php';//window.location.href = 'new.php';
//$("#div").html(r);
},
});
但是我不清楚:
谢谢!
AJAX不用于重定向。 您只能使用以下代码进行重定向:
window.location.href = 'new.php';
AJAX是通过异步模式向服务器发出请求。 您的问题的答案:
window.location.href
才能重定向 该代码的作用是调用ajax.php,并且此调用成功(在这种情况下,除非ajax.php语法错误或发送错误标头,否则始终如此),它将执行成功函数。 成功函数将重定向到new.php。
如果您只想在javascript中进行重定向,则要做的只是:
window.location = 'http://somewhere';
您可能不需要上面发布的所有代码。
让我清楚地解释你
$.ajax({
type: "POST",
url: "ajax.php",
data: dataString,
success: function(r)
{
window.location = 'new.php';//window.location.href = 'new.php';
//$("#div").html(r);
},
});
$ .ajax这个函数实际上向文件发出ajax请求。
你的问题
答案1
这行的意思是url:ajax.php ...在这种情况下,您实际上想要发出ajax请求的代码是向文件ajax.php发出ajax请求的代码
答案2
不,这与重定向完全分开。
答案3
不,如果您使用abc.html更改ajax.php,那么它将不会重定向..更改后,它将向abx.html发送ajax请求
和重定向...使用简单的代码
window.location.href = 'filename'; // replace filename with any file like new.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.