Answer the question
In order to leave comments, you need to log in
How to overcome CORS correctly?
There are two applications: a vue.js client and a laravel backend. They work on the same machine and are distributed over different domains, the backend is cool on client.my (openserver), and the client is on localhost: 9000 (nodejs)
On the server side there is a middleware that allows cross-domain requests for all api requests.
public function handle($request, Closure $next)
{
\Debugbar::disable();
header('Access-Control-Allow-Origin: *');
return $next($request);
}
addContacts ({ commit, state }, contacts) {
Vue.http.options.emulateJSON = true
Vue.http.options.emulateHTTP = true
let data = JSON.stringify(contacts)
Vue.http.post('http://client.my/api/cart/add_contacts', {contacts: data}).then(response => {
console.log(123)
}, response => {
// error callback
})
},
completeOrder ({ commit }) {
/*
Отпрвляем завершенный заказ на сервер
*/
Vue.http.options.emulateJSON = true
Vue.http.options.emulateHTTP = true
Vue.http.post('http://client.my/api/cart/complete').then(response => {
// Очистка корзины
commit('set', { type: 'cart', items: [] })
}, response => {
// error callback
})
},
Failed to load client.my/api/cart/complete: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' localhost:9000 ' is therefore not allowed access.
Answer the question
In order to leave comments, you need to log in
Hmm ... just a POST request vue-resource does not want to send, but if you specify
That all works out as it should.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question