如何使用angular.js-resource启用cors请求

cre*_*ijn 13 cors angularjs angularjs-service

我有一个angular.js应用程序,我需要做CORS请求.

我想使用角度资源定义我的休息服务"angular",如下所述:http://docs.angularjs.org/tutorial/step_11.

但我还没有找到办法让这个工作.在谷歌上我发现了以下示例代码:http://jsfiddle.net/ricardohbin/E3YEt/,但这似乎不适用于角度资源.

这是我的app.js

'use strict';

angular.module('corsClientAngularApp', ['helloServices'])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  });
Run Code Online (Sandbox Code Playgroud)

这是我的services.js与其他服务

angular.module('helloServices', ['ngResource']).
    factory('Hello', function($resource){
  return $resource('http://localhost:8080/cors-server/hello/:name', {}, {
    query: {method:'GET', params:{name:'name'}, isArray:false}
  });
});
Run Code Online (Sandbox Code Playgroud)

这是我的main.js与控制器使用$ http,这是有效的!:

'use strict';

angular.module('corsClientAngularApp')
  .controller('MainCtrl', function ($scope, $http, Hello) {
    $http.defaults.useXDomain = true;


    $http.get('http://localhost:8080/cors-server/hello/stijn')
        .success(function(data) {
            $scope.hello = data;
        });
  });
Run Code Online (Sandbox Code Playgroud)

这是我使用角度资源的main.js的另一个版本.这不起作用:(

'use strict';

angular.module('corsClientAngularApp')
  .controller('MainCtrl', function ($scope, $http, Hello) {
    $http.defaults.useXDomain = true;
    $scope.hello = Hello.query({name:'stijn'});
  });
Run Code Online (Sandbox Code Playgroud)

这是来自工作请求的标题(来自chrome devtools):

Request URL:http://localhost:8080/cors-server/hello/stijn
Request Method:OPTIONS
Status Code:200 OK

Request Headers
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:accept, origin, x-requested-with
Access-Control-Request-Method:GET
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31

Response Headers
Access-Control-Allow-Headers:accept, origin, x-requested-with
Access-Control-Allow-Methods:GET
Access-Control-Allow-Origin:*
Content-Length:0
Date:Thu, 25 Apr 2013 10:42:34 GMT
Server:Apache-Coyote/1.1
Run Code Online (Sandbox Code Playgroud)

这些是来自NOt工作请求的标题:

Request URL:http://localhost/cors-server/hello/stijn
Request Method:OPTIONS
Status Code:200 OK

Request Headers
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:accept, origin, x-requested-with
Access-Control-Request-Method:GET
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31


Response Headers
Allow:GET,HEAD,POST,OPTIONS,TRACE
Connection:Keep-Alive
Content-Length:0
Content-Type:text/plain
Date:Thu, 25 Apr 2013 10:41:12 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.22 (Win32)
Run Code Online (Sandbox Code Playgroud)

使用angular-resources时,看起来请求url是错误的.但为什么?

谢谢!

cha*_*tfl 7

$resource使用冒号接受参数的URL .因此,在url中使用port时,需要将冒号转义为端口.这在$ resource docs中有解释

  • @cremersstijn $ resource期望返回具有属性的对象.它通过将每个字符位置指定为以字符作为值的属性来强制将字符串强制转换为该对象. (3认同)
  • 究竟在资源文档中,描述了逃避端口的情况? (2认同)