欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

浅谈angular.js跨域post解决方案

程序员文章站 2022-07-06 21:26:56
跨域,前端开发中经常遇到的问题,angularjs实现跨域方式类似于ajax,使用cors机制。 下面阐述一下angularjs中使用$http实现跨域请求数据。...

跨域,前端开发中经常遇到的问题,angularjs实现跨域方式类似于ajax,使用cors机制。

下面阐述一下angularjs中使用$http实现跨域请求数据。

angularjs xmlhttprequest:$http用于读取远程服务器的数据

$http.post(url, data, [config]).success(function(){ ... });
$http.get(url, [config]).success(function(){ ... });
$http.get(url, [config]).success(function(){ ... });

一、$http.jsonp【实现跨域】

1. 指定callback和回调函数名,函数名为json_callback时,会调用success回调函数,json_callback必须全为大写。

2. 指定其它回调函数,但必须是定义在window下的全局函数。url中必须加上callback。

二、$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.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;
 });

四、实现方式

跨域方式一【jsonp】:

方法一:

$http.jsonp("http://localhost/sitesettings/getbadgeinfo.pt?jsonp=json_callback&siteid=137bd406").success(function(data){ ... });
// the name of the callback should be the string json_callback.

方法二【返回值,需要使用对应callback方法接收,但如何置于$scope?】:

$http.jsonp("http://localhost/sitesettings/getbadgeinfo.pt?jsonp=badgeabc&siteid=137bd406");
function badgeabc(data){ ... }
public string execute() throws exception { 
 string result = fail;
 response.setheader("", "");
 sitehandleraction sitehandleraction = (sitehandleraction)beansfactory.getbean(sitehandleraction.class);
 badgehandleraction badgehandleraction = (badgehandleraction)beansfactory.getbean(badgehandleraction.class);
 if("".equals(siteid) || siteid == null || stringutils.isblank("jsonp")){
 result = fail;
 }else{
 site site = sitehandleraction.find(siteid);
 userbadgestatus userbadgestatus = badgehandleraction.getuserbadgestatus(site.getid());
 if(userbadgestatus != null){
  result = "{\"t\":"+userbadgestatus.getstyle()+",\"l\":"+userbadgestatus.getsuspend_location()+",\"s\":"+site.getid()+"}";
  jsonobject jsonobj = jsonobject.fromobject(result);
  string json = jsonobj.tostring();
  result = jsonp + "(" + json + ")";
 }
 }
 printwriter write = response.getwriter();
 write.print(result);
 write.flush();
 write.close();
 return none;
}

跨域方式二【$http.get()】:

function getadustrycontroller($scope,$http){
 $http.get('http://localhost/ajax/getallindustrycategoty.pt?languagecolumn=name_eu').success(function(data){
 $scope.industries = data;
 });
}

跨域方式三【$http.post()】:

function getadustrycontroller($scope,$http){
 $http.post('http://localhost/ajax/getallindustrycategoty.pt',{languagecolumn:'name_eu'},{'content-type':'application/x-www-form-urlencoded'}).success(function(data){
 $scope.industries = data;
 });
}
// java端支持跨域请求
public string execute(){
 response.setheader("access-control-allow-origin", "*"); //允许哪些url可以跨域请求到本域
 response.setheader("access-control-allow-methods","post"); //允许的请求方法,一般是get,post,put,delete,options
 response.setheader("access-control-allow-headers","x-requested-with,content-type"); //允许哪些请求头可以跨域
 
 sitehandleraction sitehandler = (sitehandleraction) beansfactory.getbean(sitehandleraction.class);
 list list = sitehandler.getallindustrycategory(); //所有的分类集合
 jsonarray jsonarray = jsonarray.fromobject(list); //将list转为json
 string json = jsonarray.tostring(); //转为json字符串
 try {
 printwriter write = response.getwriter();
 write.print(json);
 write.close();
 } catch (ioexception e) {
 e.printstacktrace();
 }
 return none;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。