19

I am using a HTML form to submit to a jQuery validation which then sends information to a PHP processing page through ajax.

Everything works 100% apart from when everything comes back true I can't get the page to redirect to a success page.

The code I have is:

$.post(url,{ username: value_login.val(), firstname: value_firstname.val(), lastname: value_lastname.val(), email: value_email.val(), password: value_password.val()} , function(data) {

    if(data == 'success'){

        window.location.href = "http://example.com/Registration/Success/";
    } else {
       $('#error').text('error');
    }

});

I'm thinking you can't redirect using the window.location.href function.

TRiG
  • 10,148
  • 7
  • 57
  • 107
Robert
  • 907
  • 4
  • 13
  • 32
  • Did you `alert(data)` to actually see what value is returned? – Selvakumar Arumugam Jan 29 '13 at 20:16
  • 2
    `console.log(data)`. Stop using `alert()` for debugging lol. – crush Jan 29 '13 at 20:17
  • 1
    please also tell what is the error that you are getting on running the above code...Also we need to know the format of the data that is getting returned by the server...please tell that – Arshabh Agarwal Jan 29 '13 at 20:19
  • @arshA the error on the console states: Failed to load resource: the server responded with a status of 404 (Not Found) http://xxx.com/Registration/Success/ but this cant be because there is a url for this – Robert Jan 29 '13 at 20:22
  • 1
    when you get an error like that that means either the URL is wrong or server is down.... open a new tab and paste the url in the address bar to see if you can access the server – Arshabh Agarwal Jan 29 '13 at 20:25
  • @Robert - When I follow [that URL](http://xxx.com/Registration/Success/), I also get a 404. – Joseph Silber Jan 29 '13 at 20:27
  • 1
    so that means that the url does not exist.. that post request will never get called – Arshabh Agarwal Jan 29 '13 at 20:28
  • @arshA that is because i dont own the xxx.com lol it is on my local machine so i do not have an external url to give. So instead of giving http://Localhost/ i gave xxx.com – Robert Jan 29 '13 at 20:34
  • Possible duplicate of [How can I make a redirect page using jQuery?](http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-using-jquery) – 000 Dec 16 '15 at 09:26

4 Answers4

47

You forgot the HTTP part:

window.location.href = "http://example.com/Registration/Success/";
TRiG
  • 10,148
  • 7
  • 57
  • 107
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • Sliber this was just a typo on the demo code on here. I have the http on live code – Robert Jan 29 '13 at 20:18
  • 1
    @Robert - Does your server return a string? Can you show us your server-side code? – Joseph Silber Jan 29 '13 at 20:19
  • everything works fine apart from it gives an error Failed to load resource: the server responded with a status of 404 (Not Found) xxx.com/Registration/Success but this is not true, is it trying to find a file and trying to retrieve data? – Robert Jan 29 '13 at 20:23
  • @Robert - Then the `url` you're passing to `$.post` is wrong. – Joseph Silber Jan 29 '13 at 20:24
  • 1
    your web service should return a data if you are specifying a data == success. it will definitely look for some response from server. – Arshabh Agarwal Jan 29 '13 at 20:27
8

Via Jquery:

$(location).attr('href','http://example.com/Registration/Success/');
Razan Paul
  • 13,618
  • 3
  • 69
  • 61
1

I found out why this happening.

After looking at my settings on my wamp, i did not check http headers, since activated this, it now works.

Thank you everyone for trying to solve this. :)

Robert
  • 907
  • 4
  • 13
  • 32
0

This is a shorthand Ajax function, which is equivalent to:

$.ajax({  type: "POST",
           url: url,  
          data: { username: value_login.val(), firstname: value_firstname.val(), 
                  lastname: value_lastname.val(), email: value_email.val(),
                  password: value_password.val()
                },
          dataType: "json"
       success: success// -> call your func here  
      });

Hope This helps

Nick
  • 4,192
  • 1
  • 19
  • 30