使用 axios -Vue.js 删除请求

Delete request with axios -Vue.js

我在 Vue.js 客户端中 运行 DELETE 请求时遇到问题。这是我现在的代码,我只是硬编码了 link 以便尝试一下。

  deleteCompany(){
    axios.delete('http://localhost:9292/companies/1')
    .then(response =>{
      console.log(response);
    });
  }

在后端,我有一个内置 Ruby 和 Sinatra 的服务器,这是删除的方法:

#delete a company
delete '/companies/:id'do 
  content_type :json
  company = Company.get params[:id]
  if company.destroy
    status 200
    json'Company was deleted'
  else
    status 500
    json 'There was problem removing the company'
  end
end

我尝试使用 curl 和 Postman,它正在工作,但是当我尝试从客户端执行它时,它给我一个 CORS 错误,尽管 POST 等其他方法正在工作:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9292/companies/1. (Reason: Did not find method in CORS header ‘Access-Control-Allow-Methods’).

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9292/companies/1. (Reason: CORS request did not succeed).

您需要这样的东西来为您的服务器配置跨源请求 https://github.com/jdesrosiers/sinatra-cors

这里也是一篇好文章 https://medium.com/addval-labs/adding-cors-configuration-to-a-sinatra-app-1ed426e2c028

https://gist.github.com/karlcoelho/17b908942c0837a2d534