0510日重点:$http.jsonp实现angular请求的跨域问题

1.Angularjs中的$http和anjax如出一辙,当遇到跨域问题,可以用jsonp的方法解决。(ajax的jsonp见0412日笔记)

注意,我们的callback是固定的,即JSON_CALLBACK,尽量不要去做任何改动

参考文档:http://www.cnblogs.com/wwolf/p/5510936.html

 

 

二. 在服务器端设置允许在其他域名下访问

 1.$http.get

1) 在服务器端设置允许在其他域名下访问

response.setHeader("Access-Control-Allow-Origin", "*"); //允许所有域名访问
response.setHeader("Access-Control-Allow-Origin", "http://www.123.com"); //允许www.123.com访问

2) AngularJS端使用$http.get()

实现:

$http.get('http://localhost/ajax/getAllIndustryCategoty.pt?languageColumn=name_eu').success(function(data){
	$scope.industries = data;
});

2. $http.post

1)在服务器端设置允许在其他域名下访问,及响应类型、响应头设置

response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods","POST");
response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");

2) AngularJS端使用$http.post(),同时设置请求头信息

实现:

$http.post('http://localhost/ajax/getAllIndustryCategoty.pt',{languageColumn:'name_eu'},{'Content-Type':'application/x-www-form-urlencoded'}).success(function(data){
	$scope.industries = data;
});

 

posted on 2017-05-10 14:34  水月纯明  阅读(178)  评论(0)    收藏  举报

导航