CORS Configuration Puzzle in AngularJs
CORS (Cross-Origin Resource Sharing) allows JavaScript from one origin (e.g., your website) to access resources from a different origin (e.g., an API). However, it must be enabled on the server side.
The configuration you provided attempts to enable CORS on the client side, but it's ineffective. The following code snippets demonstrate what you tried:
myApp.config(function($httpProvider) { $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With']; }); myApp.service('dataService', function($http) { delete $http.defaults.headers.common['X-Requested-With']; ... });
This configuration allows cross-origin requests but fails because the server needs to respond with appropriate CORS headers to grant permission.
The error message "XMLHttpRequest cannot load URL. Origin not allowed by Access-Control-Allow-Origin" indicates that the server's response lacks the necessary CORS headers. To resolve the issue, you need to configure CORS on the server that hosts the API you're trying to access.
The provided links discuss client-side CORS handling, which is not applicable in your case. The server must implement CORS to allow access to its resources from a different origin.
The above is the detailed content of Why Doesn't My AngularJS App's Client-Side CORS Configuration Work?. For more information, please follow other related articles on the PHP Chinese website!