1

my axios code:

 const instance = axios.create({
 baseURL: process.env.BASE_API,
 timeout: 5000,
 withCredentials: true,
 headers: {
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
})

function get (url, getData) {
 return instance.get(url, {
 params: getData
 })
}

function post (url, postData) {
 return instance.post(url, qs.stringify(postData))
}

function put (url, putData) {
 return instance.put(url, qs.stringify(putData))
}

export default {
 get: get,
 post: post,
 put: put
}

Post request with content-type': 'application/x-www-form-urlencoded; charset=UTF-8 is useful

However, when using PUT, the request header does not have a content-type': 'application/x-www-form-urlencoded; charset=UTF-8 Causes the put request to become an options

htl
  • 25
  • 9

1 Answers1

0

It's not so clear from your question what exactly you are trying to ask. I'll assume you want your PUT request to actually send a PUT request instead of just an OPTIONS request. I'm also assuming that you are making requests to an API that you control.

I had the same problem (i.e. I was only seeing OPTIONS requests when I tried to make PUT calls) and I discovered that in my API I did not have the PUT options enabled in my CORS settings. I'm using rails so all I had to do is add :put to my cors middleware:

config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*', :headers => :any, :methods => [:get, :post, :put, :options]
  end
end

I figured this out based on this answer

Joe Sasson
  • 441
  • 3
  • 8
  • I am using the spring boot and set the cross-domain settings put but can not afford to use – htl Jun 05 '18 at 05:39
  • Sorry, I don't understand what you mean. You cannot _afford_ to use? I'm not familiar with Spring, but [this](https://spring.io/guides/gs/rest-service-cors/#_global_cors_configuration) looks like a good resource. – Joe Sasson Jun 05 '18 at 13:43
  • mine spring boot Cross-domain settings,POST,PUT,GET,but mine vue PUT request Was converted to options,Post is easy to use – htl Jun 06 '18 at 14:16