AnglarJSはどうやってクロスドメイン要求を実現しますか?
4998 ワード
ドメインを横断して、フロントエンドの開発でよく発生する問題に対して、AnglarJSはドメインを横断する方式をAjaxに類似することを実現して、CORSのメカニズムを使用します。
次にAnglarJSにおいて$httpを使用してクロスドメイン要求データを実現します。
AnglarJS XMLtpRequest:$httpはリモートサーバのデータを読み出すために使用されます。
1.
2.他のコールバック関数を指定しますが、windowで定義されるグローバル関数でなければなりません。urlに
二、$http.get【クロスドメインの実現】
1.サーバー側での設定は他のドメイン名でのアクセスが可能です。
1.サーバ側での設定は、他のドメイン名でのアクセスを許可し、応答タイプ、応答ヘッダの設定を許可する。
クロスドメイン方式の一【
方法1:
以上が本文の全部です。本文の内容はアングラーJSを使って勉強するのに役に立ちます。
次にAnglarJSにおいて$httpを使用してクロスドメイン要求データを実現します。
AnglarJS XMLtpRequest:$httpはリモートサーバのデータを読み出すために使用されます。
$http.post(url, data, [config]).success(function(){ ... });
$http.get(url, [config]).success(function(){ ... });
$http.get(url, [config]).success(function(){ ... });
一、$http.jsop【クロスドメインの実現】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.AnglarJS端使用$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.AnglarJS端は$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
】:方法1:
$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.
方法2【戻り値は、対応するcalback方法を使用して受信する必要があるが、$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;
}
クロスドメイン方式2【$http.get()
】
function getAdustryController($scope,$http){
$http.get('http://localhost/ajax/getAllIndustryCategoty.pt?languageColumn=name_eu').success(function(data){
$scope.industries = data;
});
}
クロスドメイン方式3【$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;
}
締め括りをつける以上が本文の全部です。本文の内容はアングラーJSを使って勉強するのに役に立ちます。