Mihai Tirtara
Mihai Tirtara

Reputation: 33

Delete request with axios -Vue.js

I have a problem while running the DELETE request in a Vue.js client. This is my code for now I just hardcoded the link in order to try it out.

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

On the backend I have a server built in Ruby with Sinatra and this is the method for DELETE:

#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

I tried with curl and Postman and it is working but when I try to do it from the client it gives me a CORS error although the other methods such as POST are working:

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).

Upvotes: 1

Views: 305

Answers (1)

Linh Nguyen
Linh Nguyen

Reputation: 947

You need something like this to configure Cross Origin Request for your server https://github.com/jdesrosiers/sinatra-cors

Also a good article here https://medium.com/addval-labs/adding-cors-configuration-to-a-sinatra-app-1ed426e2c028

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

Upvotes: 1

Related Questions