1

I am using Ajax to post a user subscription data to backend. Based on the information provided by the user server can respond with either 302 (success) or 200 (failure). My Ajax request is below:

 $.ajax({
                url : form.action,
                type: "POST",
                data: $('#real-payment-form').serialize(),
                complete: function(xhr,textStatus){
                    console.log("complete")
                    console.log(xhr.status)
                    if (xhr.status = '302'){

                    $('#payment-confirmation').modal()}
                    }
                    else {
                        alert('wrong details')}
                })
        }

But whatever the server responds with xhr.status is always 200. Can anyone point out what I am doing wrong. How can I pick up the 302 requests using ajax.

Umer
  • 250
  • 4
  • 14
  • https://stackoverflow.com/search?q=ajax+302 – freedomn-m Sep 12 '17 at 08:26
  • 302 isn't a success code, it's [redirect](https://en.wikipedia.org/wiki/HTTP_302), which the browser should handle transparently (which doesn't make much sense in an ajax call) - so jquery is not including it in `complete:` – freedomn-m Sep 12 '17 at 08:26
  • After reviewing numerous SO answers, looks like this one has the best answer for you: https://stackoverflow.com/a/2573589/2181514 – freedomn-m Sep 12 '17 at 08:33
  • @freedomn-m, in my case browser is not following the redirect. And I need to read the status code to display a message to user before redirecting the user to another page. Is it possible in ajax to halt a 302 and follow it after performing certain actions e.g a modal or pop-up showing status of user's action. – Umer Sep 12 '17 at 13:11

0 Answers0