dpvm7231
2013-07-01 17:16
浏览 54

使用jQuery AJAX重定向用户按钮点击

I am new to jQuery AJAX function. I want to redirect my user to a webpage when he clicks a button. Please do not tell me to target the button to the webpage as I have to do some working in the php file... My current code is :-

TEST.html file :-

<html>
<head>
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").load("test2.php");
  });
});
</script>
</head>

<button>Get External Content</button>
<div id="div1"></div>

</html>

Now my test2.php file is the following :-

<?php header("Location:http://google.com"); ?>

I am a beginner in AJAX jQuery. So, please do not downvote my post although it may sound silly. My code, for obvious reasons is not working. Please help me. Thanks in advance. Any help will be appreciated.

图片转代码服务由CSDN问答提供 功能建议

我是jQuery AJAX函数的新手。 我想在用户点击按钮时将用户重定向到网页。 请不要告诉我将按钮定位到网页,因为我必须在php文件中进行一些工作...我当前的代码是: -

TEST.html文件: - < / p>

 &lt; html&gt; 
&lt; head&gt; 
&lt; script type =“text / javascript”src =“jquery-1.4.4.min.js”&gt;&lt;  /script>
<script>
$(document).ready(function(){
 $(“button”)。click(function(){
 $(“#div1”)。load(“test2  .php“); 
}); 
}); 
&lt; / script&gt; 
&lt; / head&gt; 
 
&lt; button&gt;获取外部内容&lt; / button&gt; 
&lt; div id =”div1“  &gt;&lt; / div&gt; 
 
&lt; / html&gt; 
   
 
 

现在我的test2.php文件如下: - \ n

 &lt;?php header(“Location:http://google.com”);  ?&gt; 
   
 
 

我是AJAX jQuery的初学者。 所以,请不要低估我的帖子,虽然听起来可能很傻。 我的代码,由于显而易见的原因不起作用。 请帮我。 提前致谢。 任何帮助将不胜感激。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • duanjiongzhen2523 2013-07-01 17:19
    已采纳

    You cant redirect with an AJAX call. You can either create a simple link or link to a PHP page which then redirects the user on. Using AJAX will only let you manipulate this page you are on.

    Of course, you can always redirect with simple Javascript as well.

    window.location = '/my_url.php';
    

    EDIT: In response to your comment question, what I would do is use the .get() function with the data parameter, check what has been returned from the PHP page if server side validation is required and then if I am happy with the result, redirect.

    If no server side validation is needed, there is no need for AJAX.

    点赞 打赏 评论
  • douna3367 2013-07-01 17:21

    Look at it this way: AJAX is requesting and reading the .php file. When AJAX sees the header("Location: ...") line, it redirects the AJAX request. Put simply, AJAX can't be used for redirects.

    点赞 打赏 评论
  • dongwuxie7976 2013-07-01 17:21

    You can use the complete callback to redirect the user after the AJAX query has completed like so:

    $(document).ready(function(){
      $("button").click(function(){
        $("#div1").load("test2.php", 
          complete: function(){
            window.location = 'http://new_url';
          }
        );
      });
    });
    

    That being said if you just want to redirect a user on a button click there are better ways to do that.

    点赞 打赏 评论
  • duanchi4184 2013-07-01 17:26
    $("#div1").load("test2.php");  
    

    appends the contents which are returned from test2.php , it will not redirect. The test2.php should return a link and populate the div with the link. Then write a callback on ajax success which will redirect using window.location = target , the target being the link which was dynamically loaded in the div1

    点赞 打赏 评论

相关推荐 更多相似问题