使用AngularJS和手机演示应用程序时,我试试这个:
var phonecatServices = angular.module('phonecatServices', ['ngResource']);
phonecatServices.factory('Phone', function($http) {
var myService = {
query: function() {
// $http returns a promise, which has a then function, which also returns a promise
var promise = $http.get('http://192.122.122.1:4040/data/json/').then(function (response) {
// The then function here is an opportunity to modify the response
console.log(response.data);
// The return value gets picked up by the then in the controller.
return response.data;
});
// Return the promise to the controller
return promise;
}
};
return myService;
});
然后我按照原始示例尝试这种替代方法:
phonecatServices.factory('Phone', ['$resource',
function($resource){
ar x= $resource('http://192.122.122.1:4040/data/json/', {}, {
query: {method:'GET', isArray:false}
});
console.log(x)
return x;
}]);
然后我在Chrome和Firefox中得到这个:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.
HTTP/1.1 302 Found
我在Jetty servlet中定义了这些:
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate")
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, HEAD");
response.addHeader("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
response.addHeader("Access-Control-Max-Age", "1728000");
当我卷曲时:
User-Agent: curl/7.35.0
Host: 192.122.122.1:4040
Accept: */*
HTTP/1.1 200 OK
Content-Type: text/json;charset=UTF-8
Cache-Control: no-cache, no-store, must-revalidate
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE, HEAD
Access-Control-Allow-Headers: X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept
Access-Control-Max-Age: 1728000
Content-Length: 38
* Server Jetty(8.1.14.v20131031) is not blacklisted
Server: Jetty(8.1.14.v20131031)
{
"a" : "b"
}
请有人帮我这个吗?
答案 0 :(得分:0)
原来我的servlet被绑定了
http://192.122.122.1:4040/data/json/
我正在打电话
http://192.122.122.1:4040/data/json (without the last slash) .
这导致重定向-302 - 角度不满意。