Home > Enterprise >  Axios is sending OPTIONS instead of POST to Django
Axios is sending OPTIONS instead of POST to Django

Time:09-21

I'm using Django Rest Framework with a front-end in Vue. I'm using axios to make POST requests to Django, but the Django server is receiving OPTIONS requests, as you can see in the server log.

"OPTIONS /save/ HTTP/1.1" 200 0
Broken pipe from ('127.0.0.1', 59396)

The problem doesn't appear to be in the server, since CORS is configured correctly and I can send a POST request with Postman. Here's the javascript code

async save() {
  let data = {
    "name": this.name,
    "title": this.title,
  }
  let config = {
    header : {
      "Content-Type": "application/json",
    }
  }   
  response = await axios.post(
    'http://127.0.0.1:8000/save/',
    data,
    config
  )
}

    

CodePudding user response:

try with headers instead header in config

CodePudding user response:

The browser will always send an OPTIONS request when the back end is on a different domain. It might be worth troubleshooting your CORS config to ensure it is successfully processing the OPTIONS request and returning the right response. Only once the OPTIONS request returns 200 will the browser send your POST request. See this excellent article on CORS for more detail: https://codeburst.io/cors-story-of-requesting-twice-85219da7172d

  • Related