如何

2014-01-08 66 views
0

我對這個代碼的工作成功的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" 
} 

如何將用戶重定向到外部網站讓說www.google.com如果只有返回狀態是成功的json?

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

笑似乎回答我的問題....

在這裏,我如何管理,最終做到這一點..

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>'); 


     }, 
+0

重定向在普通的JavaScript是 'window.location的那樣簡單=「http://www.facebook.com 「' 雖然我不知道jquery。 – Obversity

回答

0

在你的成功塊添加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 
     } 
0

改寫像下面的成功回調代碼,

你需要使用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"; 
    }, 
1

試試這個

$("#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"; 
} 
+0

不工作...我得到「未定義」的錯誤... –

+0

如果返回類型是json或不 –

0

試試這個:

<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' 
});