大佬教程收集整理的这篇文章主要介绍了angularjs – 如何使用angular.js-resource启用cors请求,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用角度资源定义我的休息服务“角度”,如下所述: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: '/' }); });
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} }); });
这是我的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; }); });
这是我使用角度资源的main.js的另一个版本.这不工作:(
'use Strict'; angular.module('corsClientAngularApp') .controller('MainCtrl',Hello) { $http.defaultS.UseXDomain = true; $scope.Hello = Hello.query({name:'stijn'}); });
这是工作请求中的标题(来自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,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
这些是NOt工作请求中的头文件:
request URL:http://localhost/cors-server/Hello/stijn request Method:OPTIONS Status Code:200 OK request Headers Accept:*/* Accept-Charset:ISO-8859-1,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)
使用角度资源时,请求url看起来是错误的.但为什么?
谢谢!
以上是大佬教程为你收集整理的angularjs – 如何使用angular.js-resource启用cors请求全部内容,希望文章能够帮你解决angularjs – 如何使用angular.js-resource启用cors请求所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。