Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angular app调用rest服务时端口号为何被切断_Rest_Angularjs - Fatal编程技术网

angular app调用rest服务时端口号为何被切断

angular app调用rest服务时端口号为何被切断,rest,angularjs,Rest,Angularjs,我有休息服务: http://localhost:3000/api/brands 当我从web浏览器测试它时,它工作得很好 我在角度服务中使用它: var motoAdsServices = angular.module('motoAdsServices', ['ngResource']); motoAdsServices.factory('Brand', ['$resource', function($resource) { return $resource('http://loc

我有休息服务:

http://localhost:3000/api/brands
当我从web浏览器测试它时,它工作得很好

我在角度服务中使用它:

var motoAdsServices = angular.module('motoAdsServices', ['ngResource']);

motoAdsServices.factory('Brand', ['$resource', function($resource) {
    return $resource('http://localhost:3000/api/:id', {}, {
      query: {
        method: 'GET',
        params: {
          id: 'brands'
        },
        isArray: true
      }
    });
  }]);
调用时,我有错误,因为在reqest URL中我没有端口号:

Request URL: http://localhost/api/brands
为什么端口号被切断?切吧

其中写道:

参数化URL模板,参数前缀为:as in/user/:username。如果您正在使用带有端口号的URL(例如
http://example.com:8080/api
),它将得到尊重

更新


我使用angular版本1.0.8(感谢@KayakDave的关注),但适用于版本1.2

它有一个冒号,因此被剥离,有点像:id。如果你逃出它,你应该没事。试试
http://localhost\:3000/api/:id
。您可能会在路线或其他地方再次遇到此问题。 在某些情况发生变化时,这种行为是有可能发生的


更新:
http://localhost3000:/API:I/<代码> < /P> < P>因为角度截取URL并考虑<代码>:任何< /代码>作为您应该传递给它的参数。

一个简单的破解方法是将
\:3000
放在你的url中

motoAdsServices.factory('Brand', ['$resource', function($resource) {
    return $resource('http://localhost:3000\:3000/api/:id', {}, {
        query: {
            method: 'GET',
            params: {
                id: 'brands'
            },
            isArray: true
        }
    });
}]);

资源插件问题的快速修复:

 var fixedTargetUrl = TARGET_URL.replace(/(:\d{4})/, "\\$1");

谢谢我的拼写错误,我更新了它。我有三项服务
品牌
国家
广告
。它们都不起作用。所以问题仍然存在。你用的是什么版本的angular?非转义端口号支持是在1.2(这里是PR:)Angular 1.0.8中添加的。所以这个版本不尊重端口。但是`\`对我不起作用。我用双反斜杠转义了端口号,它起作用了。
http://localhost\:3000/api/:id
它对我不起作用。你确定吗?医生说了些不同的话。在我问题的末尾读一下。我用双反斜杠转义了端口号,它可以工作。这是一个问题的解决方案。现在我有了:Origin
http://localhost:8000
是访问控制允许原点不允许的。但我的问题已经解决:)[建议在1.0.2、1.0.3、1.1.0和1.1.1中转义$resource。感谢您节省了我的时间。已经花了4个小时搜索此问题的原因:-/