Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript POST fetch sends as OPTIONS

I am learning JavaScript, and I am making an account log-in page as a learning project. The server is using Python’s Flask. The fetch function doesn’t seem to be working as I want it to. For one, it is sending an options request instead of a POST, even though I specified POST. Another thing is that the server isn’t receiving the data, it comes up as blank. Here is the code:

var content = JSON.stringify({'username': username, 'password': password}) //username and password are user inputs, I had them print and these work fine.
var response = fetch(url+"/api/login", {
      method: "POST",
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: content
    })
.then(response => {
  console.log("Success") //this doesn't print.
})

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

thats a preflight request

Handle the options request by passing the correct CORS Headers back.

Then also handle the post request, both of these are needed due to the new CORS standards for APIs and Websites.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading