Search code examples
javascripthttppostfetch-api

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

  • 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.