Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS request did not succeed

Tags:

laravel

vue.js

I have a problem when I want to create an authentication system using VueJs as the frontend (http://localhost:8080/#/login) and Laravel 5.6 as the backend. When I try to submit login form using the api login url http://127.0.0.1:8000/api/v1/login, I get the error message:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:8000/api/v1/login. (Reason: CORS request did not succeed).

I don't know how to solve this problem.

Could anyone here help me to solve my problem?

NOTE : I have to install laravel-cors before

like image 880
Shinoda_ Avatar asked Jul 13 '18 18:07

Shinoda_


People also ask

How do you solve a CORS error?

Cross-Origin Resource Sharing (CORS) errors occur when a server doesn't return the HTTP headers required by the CORS standard. To resolve a CORS error from an API Gateway REST API or HTTP API, you must reconfigure the API to meet the CORS standard.

Why CORS is not working?

Or, your API fails and shows a CORS error in the console. This happens because the same-origin policy is part of the browser's security model which allows websites to request data from APIs of the same URL but blocks those of different URLs. Browsers do this by adding an ORIGIN key in the request.

What's wrong with this Cors request?

(Reason: CORS request did not succeed)." "What went wrong? The HTTP request which makes use of CORS failed because the HTTP connection failed at either the network or protocol level. The error is not directly related to CORS, but is a fundamental network error of some kind.

How do I know if CORS is not configured?

If the CORS is not configured you can see the error on the console’s browser indicating that the request was blocked: “ Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at $somesite ”. When do I know if the requests are from the same origin?

Why am I getting a network error when I try Cors?

The error is not directly related to CORS, but is a fundamental network error of some kind. In many cases, it is caused by a browser plugin (e.g. an ad blocker or privacy protector) blocking the request. Other possible causes include:

How to fix cross origin request security (Cors) error in Firefox?

Perform the operation which is throwing Cross Origin Request Security (CORS) error. Open firebug and copy the URL which is throwing Cross Origin Request Security (CORS) error. Load the same URL in another tab in same Firefox browser. Once you open the URL in another tab will ask you to add the certificate.


4 Answers

This is an old question, but I'll reply nonetheless.

For me this error was caused by a self-signed certificate. If you open developer tools, select the network tab, click the call that failed CORS you can see the security tab. Click it to open it. If a cert is giving you problems the text "An error occurred: SEC_ERROR_INADEQUATE_KEY_USAGE" should be visible.

To resolve this just go to the URL that gave you the CORS error, and accept the cert manually.

like image 176
user11864445 Avatar answered Sep 27 '22 19:09

user11864445


Cross Origin Resource Sharing is a mechanism that uses additional HTTP headers to tell a browser to allow the web application running on one origin (client) have permission to access selected resources from a server at a different origin.

Basically, your Vue app (http://localhost:8080) needs to be allowed access to your Laravel endpoint (http://127.0.0.1:8000/api/v1/login) This is to prevent me from hitting your Laravel endpoint from my malicious website and acting like an authenticated user.

Based on the docs, you need to add 'allowedOrigins' => ['*'], but that means you're opening up your backend to all requests. That's fine if it's a public API but in this context it doesn't sound like you want that. Instead, in this case it would be 'allowedOrigins' => ['localhost:8080'], so that your Vue app can consume your Laravel server.

like image 34
Dan Hilton Avatar answered Sep 27 '22 19:09

Dan Hilton


You have to use either localhost or 127.0.0.1 for all the requests. In general in your code you should make calls to the server by just appending the URI to the current host, without re-adding the host and port in the URI string. If you load your page from a given host, for example 127.0.0.1 and then try to make an AJAX request to another host, for example www.host.com, the request gets blocked to prevent XSS attacks

like image 32
zizzo Avatar answered Sep 27 '22 20:09

zizzo


It sounds like you are running this in dev mode via webpack currently? If that is correct and your workflow is that you are going to build the Vue application and have it co-reside with your Laravel backend then you just need to update config/index.js to have a proxyTable entry that forwards webpack requests to the correct dev Laravel backend server.

This would look something like this.

module.exports = {
  dev: {
    proxyTable: {
      "/": "http://127.0.0.1:8000/api/v1/login"
    }
  }
}

There is additional information available on how this works; https://vuejs-templates.github.io/webpack/proxy.html

like image 43
bigtunacan Avatar answered Sep 27 '22 20:09

bigtunacan