简体   繁体   English

为什么我们需要放置源名称进行重定向?

[英]why do we need to place the source name to redirect?

I saw in another post ( jQuery Ajax PHP redirecting to another page ) that when using ajax to redirect to a PHP page we need to set an event like so : 我在另一篇文章( 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);
  },
});

However it's not clear to me : 但是我不清楚:

  1. why do we need to indicate the " url: "ajax.php"," 为什么我们需要指出“ url:“ ajax.php”,”
  2. should the url entry contain the name of the current file from which we're redirecting ? 网址条目是否应包含我们要从中重定向的当前文件的名称?
  3. if i'm redirecting from a file called abc.html ? 如果我要从一个名为abc.html的文件重定向? I should just replace the ajax.php with abc.html ? 我应该只用abc.html替换ajax.php吗?

Thanks! 谢谢!

AJAX is not for redirect. AJAX不用于重定向。 You can redirect only with this code: 您只能使用以下代码进行重定向:

window.location.href = 'new.php';

AJAX is to make requests to the server by asynchronous mode. AJAX是通过异步模式向服务器发出请求。 The answers to your questions: 您的问题的答案:

  1. Is the url to make requests. 是发出请求的网址。 You don't need it if your only purpose is redirect. 如果您的唯一目的是重定向,则不需要它。
  2. No if you don't will use it. 不,如果您不使用它。
  3. NO. 没有。 Only with window.location.href you can redirect 只有使用window.location.href才能重定向

What this code does is that it calls ajax.php, and when this call succeeds (which is always in this case, unless ajax.php has syntax error or sends error header), it executes the success function. 该代码的作用是调用ajax.php,并且此调用成功(在这种情况下,除非ajax.php语法错误或发送错误标头,否则始终如此),它将执行成功函数。 Success function does the redirect to new.php. 成功函数将重定向到new.php。

If you just want to do a redirect in javascript, all you have to do is: 如果您只想在javascript中进行重定向,则要做的只是:

window.location = 'http://somewhere';

you probably don't need all this code you posted above. 您可能不需要上面发布的所有代码。

let me explain you clearly 让我清楚地解释你

$.ajax({
type: "POST",
url: "ajax.php",
data: dataString,
success: function(r) 
{
   window.location = 'new.php';//window.location.href = 'new.php';
   //$("#div").html(r);
},
});

$.ajax this function actually makes ajax request to files .. $ .ajax这个函数实际上向文件发出ajax请求。

your questions 你的问题

  1. why do we need to indicate the " url: "ajax.php"," 为什么我们需要指出“ url:“ ajax.php”,”
  2. should the url entry contain the name of the current file from which we're redirecting ? 网址条目是否应包含我们要从中重定向的当前文件的名称?
  3. if i'm redirecting from a file called abc.html ? 如果我要从一个名为abc.html的文件重定向? I should just replace the ajax.php with abc.html ? 我应该只用abc.html替换ajax.php吗?

answer #1 答案1

this line means url: ajax.php ... where actually you want to make ajax request in this case code making ajax request to file ajax.php 这行的意思是url:ajax.php ...在这种情况下,您实际上想要发出ajax请求的代码是向文件ajax.php发出ajax请求的代码

answer #2 答案2

no this is totally separate from redirecting .. 不,这与重定向完全分开。

answer #3 答案3

no if you change ajax.php with abc.html then it will not redirect..after changing it will make ajax request to abc.html now 不,如果您使用abc.html更改ajax.php,那么它将不会重定向..更改后,它将向abx.html发送ajax请求

and for redirecting ... use simple code 和重定向...使用简单的代码

window.location.href = 'filename'; // replace filename with any file like new.php

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

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