2021年3月21日星期日

Javascript XHR geting response after sending POST

I'm making an XHR request from my js frontend to my PHP backend, and I'd like to display on my frontend if the backend operation was successful. Here is my code:

submitHandler = (e) => {          const reqBody = this.state; //irelevant prepared data          e.preventDefault();          const xhr = new XMLHttpRequest();          xhr.open('POST', '/response.php', true)          xhr.onload = () => {              console.log(xhr.response);          };          console.log(reqBody)          xhr.send(reqBody);      }  

My PHP backend file just var_dumps $_REQUEST But when I call submitHandler, it consoleLogs just bank var_dump instead of var_dump of my data. Is it possible to somehow get the state of the site after the POST data was already processed by the backend?

I think it might not be clear just from my previous explanation, so here is my explanation of how it should work: The user sends a form with data to the backend PHP file, backend PHP file checks if that person's name is in the database, and responds according to it. Then my frontend gets the response and shows an error or info about that person.

Thanks for help.

https://stackoverflow.com/questions/66738208/javascript-xhr-geting-response-after-sending-post March 22, 2021 at 06:26AM

没有评论:

发表评论