I am new to the React command on the โextractโ. I can use it for GET, but I had no luck using it with POST. I am running the following simple code.
onSubmit(event) { // For some reason, "fetch" is GETTING instead of POSTING. fetch('/questionare/', { method: 'post', body: JSON.stringify({greeting: "Hello"}) }).then((res) => alert(res.json)); }
The content of the POST request is processed on the internal server using the following code.
router.post('/', function(req, res, next) { res.json(req.body); });
Therefore, the web browser should create a warning window for the response, which is the exact content that I sent in the request. This did not happen. I looked through the log of my server and found that the POST request was not issued. The fetch function seems to ignore the method parameter. It always executes GET requests, even if I tell him to make a POST request. I know that in another post, they said that at the end of the URL in the request there should be a slash ( fetch () that does GET instead of POST on response-native (iOS) ). However, this did not happen. Does anyone else see what is wrong?
EDIT 3-3-2018:
As suggested, I included the headers. However, the software still sends GETs instead of POST requests. Code using "fetch" now looks like this. What else could be wrong?
onSubmit(event) { // For some reason, "fetch" is GETTING instead of POSTING. fetch('/questionare/', { method: 'post', body: JSON.stringify({greeting: "Hello"}), headers: new Headers({ 'Content-Type': 'application/json', 'Accept': 'application/json' }) }) .then(res => res.json) .then(console.log); }
post get fetch reactjs
Rachel lightwalker
source share