Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why axios send OPTIONS request method not DELETE or PUT method

Tags:

reactjs

axios


why I use axios.delete(url) or axios.put() to send request, but when I check request from NetWork in Chrome Dev Tools the Request Method is OPTIONS and Access-Control-Request-Method is DELETE or PUT like this:

enter image description here

like image 797
Hieu Vu Avatar asked Apr 29 '18 22:04

Hieu Vu


People also ask

Why is options request sent?

This pre-flight request is made by some browsers as a safety measure to ensure that the request being done is trusted by the server. Meaning the server understands that the method, origin and headers being sent on the request are safe to act upon.

How do you send a body with Delete request Axios?

To Use Axios Delete request with body and headers In ReactJS You Just need to Use axios. delete{URL,{headers:{},data:{}}} This Structure, Where You can Pass Authorization, and etc Params That you want to pass in your headers, and pass all body params in data{}. Now, You Can Easily Use Delete Request. Thank You.

Why is there an options request before post?

Prevent sending the post data, if it wont be processed This is the only reason what is valid. Using options request will prevent sending the post data to the server unnecessarily.

How do you delete in Axios?

DELETE request using axios with async/await This sends the same DELETE request using axios, but this version uses an async function and the await javascript expression to wait for the promises to return (instead of using the promise then() method as above).


1 Answers

DELETE or PUT (or other non-simple) requests first send out a preflighted OPTIONS request to determine if you're allowed to send this request. The request method is given in the Access-Control-Request-Method header.

See more here https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Simple_requests

like image 172
Phillip Avatar answered Nov 10 '22 19:11

Phillip