【问题标题】:axios put Not workingaxios put 不工作
【发布时间】:2018-11-07 13:27:45
【问题描述】:

我的 axios 代码:

 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
}

使用 content-type 发布请求': 'application/x-www-form-urlencoded; charset=UTF-8 很有用

但是,当使用 PUT 时,请求头没有 content-type': 'application/x-www-form-urlencoded;字符集=UTF-8 使 put 请求成为一个选项

【问题讨论】:

    标签: http vue.js axios put


    【解决方案1】:

    从您的问题中不清楚您到底想问什么。我假设您希望您的 PUT 请求实际发送一个 PUT 请求,而不仅仅是一个 OPTIONS 请求。我还假设您正在向您控制的 API 发出请求。

    我遇到了同样的问题(即,当我尝试进行 PUT 调用时,我只看到了 OPTIONS 请求)并且我发现在我的 API 中我没有在我的 CORS 设置中启用 PUT 选项。我正在使用 Rails,所以我所要做的就是将 :put 添加到我的 cors 中间件:

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

    我是根据this answer想出这个的

    【讨论】:

    • 我用的是spring boot,设置了跨域设置放但是用不起
    • 对不起,我不明白你的意思。你不能负担使用吗?我对 Spring 不熟悉,但 this 看起来是个不错的资源。
    • 我的spring boot 跨域设置,POST,PUT,GET,但是我的vue PUT请求被转换为options,Post好用
    猜你喜欢
    • 1970-01-01
    • 2018-05-20
    • 2019-08-14
    • 2018-12-28
    • 2017-09-07
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多