1

I was debugging my website when I realized that all my form post are receiving 302 response, but it is working fine. The way I handle the POST is through JS, using XMLHttpRequest like this:

HTML

 <form method="POST" onsubmit="return submitFn(this)">

JS

 submitFn = function(formElement) {
     var formdata = new FormData(formElement);
     var xhr = new XMLHttpRequest();
     xhr.open(formElement.method, "/formaction", true);
     xhr.addEventListener('readystatechange', function(e) { ... }
     xhr.send(formdata);

     return false;
}

As I know, return false avoid HTML POST so I can handle it with JS. Everything works fine BUT when I see "Network Activity" in Debugger Console (Chrome or FF) there is a 302 responde from "formaction" page, is that right? or am I making a mistake?

Leonardo Javier
  • 69
  • 1
  • 10

2 Answers2

0

301: Permanent redirect

302: Temporary redirect

303: Redirect and change request method to GET

307: Redirect and keep request method

302 is a warning message telling you that the URL that you are requesting may change in the future.

I found this question that is similar to what you are asking

What does HTTP/1.1 302 mean exactly?

Zeke
  • 96
  • 1
  • 7
0

Your formaction page is sending back the 302 response. It has nothing to do with your xhr code. It's likely formaction is putting a Location header in the HTTP response, which accounts for the 302. Since the request is made via xhr, your browser does not redirect, which is why everything is working OK.

tronman
  • 8,540
  • 9
  • 41
  • 47