0

I working on this code...

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

});

if the data that return is successful it appear like this :

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

and if the data that return is success but registration failed.

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

how do i redirect user to external website let say www.google.com if only the return status is success in the json ?

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

lol seem to answering my own question....

here how i manage to do it eventually ..

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


        },
Azizi Musa
  • 1,009
  • 2
  • 10
  • 31
  • Redirecting in plain javascript is as simple as `window.location = "http://www.facebook.com"` I don't know about jquery though. – Obversity Jan 08 '14 at 04:30

5 Answers5

1

Try this

$("#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";
}
Raj Mohan
  • 543
  • 9
  • 25
0

After the line of code,

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

Use the following Javascript code to redirect:

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

Why is window.location.replace() better than window.location.href?

Community
  • 1
  • 1
Rahul Desai
  • 15,242
  • 19
  • 83
  • 138
0

In your success block add 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 
        }
Rizwan Sultan
  • 187
  • 10
0

rewrite the success callback code like below,

you need to use 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";
    },
2intor
  • 1,044
  • 1
  • 8
  • 19
0

try this:

<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'
 });
Sunny Sharma
  • 4,688
  • 5
  • 35
  • 73