如何在成功 AJAX 请求数据后重定向到外部网站



我在处理这段代码...

<script>
$("#ajaxform").submit(function(e)
{
    $("#simple-msg").html("<img src='images/ajax-loader.gif'/>");
    var postData = $(this).serializeArray();
    var formURL = $(this).attr("action");
    $.ajax(
    {
        url : formURL,
        type: "POST",
        data : postData,
        success:function(data, textStatus, jqXHR) 
        {
            $("#simple-msg").html('<pre><code class="prettyprint">'+data+'</code></pre>');
        },
        error: function(jqXHR, textStatus, errorThrown) 
        {
            $("#simple-msg").html('<pre><code class="prettyprint">AJAX Request Failed<br/> textStatus='+textStatus+', errorThrown='+errorThrown+'</code></pre>');
        }
    });
    e.preventDefault(); //STOP default action
});
$("#ajaxform").submit(); //SUBMIT FORM
      $('#simple-post').click(function(){
 $('#simple-msg').css({
     'display':'block',
   'background-color':'red',    
   'font-size':'44px'
 });

});

如果返回的数据成功,则如下所示:

{
status: "Success",
msg: "You have been registered"
}

以及如果返回的数据成功但注册失败。

{
status: "Fail",
msg: "Please provide full name"
}

如何将用户重定向到外部网站 假设 JSON 中的返回状态仅为成功 www.google.com?

====

===========================================

哈哈似乎在回答我自己的问题....

这是我最终如何设法做到这一点的..

success:function(data, textStatus, jqXHR) 
        {
            var a = JSON.parse(data)
            if(a.status == 'success') window.location.href = 'http://google.com';
            else
            $("#simple-msg").html('<pre><code     class="prettyprint">'+a.status+'</code></pre>');

        },

试试这个

$("#simple-msg").html('<pre><code class="prettyprint">'+data.msg+'</code></pre>'); //if You want show only the message
if(data.status.toLowerCase == "success"){
  window.location.href = "http://www.google.com";
}

在代码行之后,

$("#simple-msg").html('<pre><code class="prettyprint">'+data+'</code></pre>');

使用以下 Javascript 代码进行重定向:

window.location.replace("http://www.google.com");

为什么window.location.replace()比window.location.href更好?

在你的成功块中添加 window.location.href = 'http://www.google.com';

success:function(data, textStatus, jqXHR) 
        {
            $("#simple-msg").html('<pre><code class="prettyprint">'+data+'</code></pre>');
            window.location.href = 'http://www.google.com';  // It redirect to google 
        }

重写成功回调代码,如下所示,

你需要使用 window.location.href = "http://www.google.com";

    success:function(data, textStatus, jqXHR) 
    {
        $("#simple-msg").html('<pre><code class="prettyprint">'+data+'</code></pre>');
        window.location.href = "http://www.google.com";
    },

试试这个:

<script>
$("#ajaxform").submit(function(e)
{
 $("#simple-msg").html("<img src='images/ajax-loader.gif'/>");
 var postData = $(this).serializeArray();
 var formURL = $(this).attr("action");
 $.ajax(
 {
     url : formURL,
     type: "POST",
     data : postData,
     success:function(data, textStatus, jqXHR) 
     {
        if(data.status=="success"){
          window.location.href="http://www.google.com"; 
        }
         $("#simple-msg").html('<pre><code class="prettyprint">'+data+'</code></pre>');
     },
     error: function(jqXHR, textStatus, errorThrown) 
     {
         $("#simple-msg").html('<pre><code class="prettyprint">AJAX Request Failed<br/> textStatus='+textStatus+', errorThrown='+errorThrown+'</code></pre>');
     }
 });
 e.preventDefault(); //STOP default action
});
$("#ajaxform").submit(); //SUBMIT FORM
   $('#simple-post').click(function(){
$('#simple-msg').css({
  'display':'block',
  'background-color':'red',    
  'font-size':'44px'
 });

最新更新