溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何解決angular.js跨域post的問題

發(fā)布時間:2021-07-20 13:59:29 來源:億速云 閱讀:131 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細講解有關(guān)如何解決angular.js跨域post的問題,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

跨域,前端開發(fā)中經(jīng)常遇到的問題,AngularJS實現(xiàn)跨域方式類似于Ajax,使用CORS機制。

AngularJS XMLHttpRequest:$http用于讀取遠程服務器的數(shù)據(jù)

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

一、$http.jsonp【實現(xiàn)跨域】

1. 指定callback和回調(diào)函數(shù)名,函數(shù)名為JSON_CALLBACK時,會調(diào)用success回調(diào)函數(shù),JSON_CALLBACK必須全為大寫。

2. 指定其它回調(diào)函數(shù),但必須是定義在window下的全局函數(shù)。url中必須加上callback。

二、$http.get【實現(xiàn)跨域】

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【實現(xiàn)跨域】

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;
 });

四、實現(xiàn)方式

跨域方式一【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轉(zhuǎn)為json
 String json = jsonArray.toString(); //轉(zhuǎn)為json字符串
 try {
 PrintWriter write = response.getWriter();
 write.print(json);
 write.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 return NONE;
}

關(guān)于“如何解決angular.js跨域post的問題”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI