Rails 5.1 API CORS problems

2.9k views Asked by At

I have a rails 5.1.6 api that generates json. I have a separate Vue.js client that aims to consume it.

The model is entities. The GET request works fine to view the index of all entities in the db. However, when I attempt to POST a new entity, I get the following error in console:

Access to XMLHttpRequest at 'http://localhost:3000/entities' from origin > 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control- Allow-Origin' header is present on the requested resource.

In the rails app, I have installed the rack-cors gem and in initializers/cors.rb:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins 'localhost:3000'
    resource '*',
      headers: :any,
      methods: %i(get post put patch delete options head)
  end
end

in routes.rb:

Rails.application.routes.draw do
  # For details on the DSL...
  post 'auth/login', to: 'authentication#authenticate'
  post 'signup', to: 'users#create'

  resources :entities do
    resources :transactions
  end
end

in the client layer:

src/services/Api.js:

import axios from 'axios'

export default() => {
  return axios.create({
    baseURL: 'http://localhost:3000'
  })
}



src/services/EntitiesService.js:

import Api from '@/services/Api'

export default {
  fetchEntities () {
    return Api().get('/entities')
  },

  addEntity (params) {
    return Api().post('entities', params)
  }
}

and

src/components/newEntity.vue:

<template>
  <div class="entities">
    <h1>Add Entity</h1>
      <div class="form">
        <div>
          <input type="text" name="title" placeholder="TITLE" v-model="title">
        </div>
        <div>
          <textarea rows="15" cols="15" placeholder="DESCRIPTION" v-model="description"></textarea>
        </div>
        <div>
          <button class="app_entity_btn" @click="addEntity">Add</button>
        </div>
      </div>
  </div>
</template>

<script>
import EntitiesService from '@/services/EntitiesService'
export default {
  name: 'NewEntity',
  data () {
    return {
      title: '',
      description: ''
    }
  },
  methods: {
    async addEntity () {
      await EntitiesService.addEntity({
        title: this.title,
        description: this.description
      })
      this.$router.push({ name: 'Entities' })
    }
  }
}
</script>
<style type="text/css">
.form input, .form textarea {
  width: 500px;
  padding: 10px;
  border: 1px solid #e0dede;
  outline: none;
  font-size: 12px;
}
.form div {
  margin: 20px;
}
.app_entity_btn {
  background: #4d7ef7;
  color: #fff;
  padding: 10px 80px;
  text-transform: uppercase;
  font-size: 12px;
  font-weight: bold;
  width: 520px;
  border: none;
  cursor: pointer;
}
</style>

what is wrong here?

1

There are 1 answers

1
user2799827 On BEST ANSWER

This is working now after including headers in the request obviously...

here is the new request:

src/services/Api.js

  import axios from 'axios'

    export default() => {
      return axios.create({
        baseURL: 'http://localhost:3000',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Access-Control-Allow-Origin': '*'
        }
      })
    }