[英]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.