Closed localgod closed 9 years ago
There is nothing wrong with the code you have provided. I created a simple static HTML page that makes a cross-origin request to the code you provided using jQuery. Everything worked as expected. Therefore, I suspect that the problem is in your angularjs app. I'm not familiar enough with angularjs to setup my own test using angularjs.
I found a jsfiddle that I was able to modify to setup a test with angularjs. Everything still seems to be working as expected. Here is the code I used.
<html>
<head>
<title>Test CORS</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.16/angular.js"></script>
<script>
angular
.module('Test', [])
.controller('corsCtrl', function ($scope, $http) {
$scope.useHttp = function() {
$http
.get('http://localhost:8000')
.success(function(data) {
alert(data.title);
});
};
});
</script>
</head>
<body>
<div ng-app="Test">
<div ng-controller="corsCtrl">
<button ng-click="useHttp()">$http.get request</button>
</div>
</div>
</body>
</html>
Hmmm. Your example dos not work on my setup (osx, firefox 38 and safari 8). Any idea?
Update: Ok I got you example working.... now to figure out why my angular example dos not:
angular.module('exampleApp')
.controller('MainCtrl', function ($scope, $http) {
$scope.myData = {};
$scope.myData.doClick = function(item, event) {
var responsePromise = $http.get("http://127.0.0.1:8000");
responsePromise.success(function(data, status, headers, config) {
$scope.myData.fromBackend = data.title;
});
responsePromise.error(function(data, status, headers, config) {
alert("AJAX failed!");
});
}
});
Turns out you can not use 127.0.0.1
but localhost
works. Strange but what can you do...
If I start my skeleton app like this:
Serving this file:
When I access the server from my angularjs app running on
127.0.0.1:10000
I get the following error in my browser:Any hints?