1

My Code:

<script>
  $('#form').submit(function() {
    $.ajax({
      type: 'POST',
      url: $(this).attr('action'),
      dataType: 'json',
      success: function(json) {
        window.location.href = "http://www.example.com";
      }
    });
    return false;
  });
</script>

The FORM:

<form id="form" class="center" action="http://localhost/products/index.php?route=checkout/cart/add" method="post">
  <input type="text" name="cname">
  <input type="hidden" name="product_id" value="51">
  <input type="submit">
</form>

When the form presses submit it goes to the action page which is just a JSON success message, what I want to do is redirect to a different page other than the action page, but my code does not seem to be working?

What exactly is wrong with my code, can someone help me fix it?

I would be so grateful if you could help me out, many thanks!

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
rakupu
  • 109
  • 1
  • 8

3 Answers3

2

You aren't posting any data which makes a POST fairly useless .

Also you have no error handler either

Try:

$(function(){
    $('#form').submit(function() {

       var $form = $(this);

        $.ajax({
            type: 'POST',
            url: $form.attr('action'),

            // data to send
            data: $form.serialize(),

            dataType: 'json',
            success: function(json) {
               window.location.href = "http://www.example.com";
            },
            error: function(){
               // do something when request fails - See $.ajax docs
            }
        })
        return false;
    });
});
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

You can used this code for error handling! You also check this question on stackOverflow for redirecting to another page using jQuery/JavaScript: click here

$('#form').submit(function() {
  var $form = $(this);
  $.ajax({
    type: 'POST',
    url: $form.attr('action'),

    // data to send
    data: $form.serialize(),
    dataType: 'json',
    success: function(json) {
      window.location.href = "http://www.example.com";
    },
    error: function (jqXHR, exception) {
      var msg = '';
      if (jqXHR.status === 0) {
        msg = 'Not connect.\n Verify Network.';
      } else if (jqXHR.status == 404) {
        msg = 'Requested page not found. [404]';
      } else if (jqXHR.status == 500) {
        msg = 'Internal Server Error [500].';
      } else if (exception === 'parsererror') {
        msg = 'Requested JSON parse failed.';
      } else if (exception === 'timeout') {
        msg = 'Time out error.';
      } else if (exception === 'abort') {
        msg = 'Ajax request aborted.';
      } else {
        msg = 'Uncaught Error.\n' + jqXHR.responseText;
      }
      alert(msg);
    },
  });
});
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Mr. Aniket
  • 83
  • 11
0

You need to have separate error and success handlerS like below. In success method you can redirect to other pages/sites (window.location.href = "http://www.EXAMPLE.com";)

var ajaxUpdateRequest = {
url: '/dotnethelpers/UpdateUsers',
dataType: 'json',
success: updateSuccessfully, //Separate method for handling success
error: showError //Separate method for handling error
}; 
Flexo
  • 87,323
  • 22
  • 191
  • 272