'Access control allow origin missing on localhost

I'm developing a vuejs application on the frontend and an API in Sinatra.

In Sinatra:

post '/login' do
  # should be pretty easy to do in Sinatra:
  response['Access-Control-Allow-Origin'] = '*'
  JSON :data => 'tadaaaaa'
end

and in Vue:

methods : {
  submitForm() {
    var myHeaders = new Headers({
      'Content-Type': 'application/json'
     });
    var myInit = {
      method: 'POST',
        headers: myHeaders,
        body: JSON.stringify({
        email: this.admEmailname,
        pass: this.admPassWord
    })
   }
   fetch('http://localhost:9292/login', myInit)
     .then(response => response.json())
  }
 }
}

I get an answer saying that access control allow origin header is missing. I tried:

curl -i -X POST -H 'Content-Type: application/json' -d '{"name": "New item", "year": "2009"}' http://localhost:9292/login

and this returns the right answer (tadaaaaa).

I don't see what I'm doing wrong.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source