クロスドメイン、フロントエンド開発で遭遇する一般的な問題。 AngularJSは、AJAXと同様のクロスドメイン法を実装し、CORSメカニズムを使用します。
以下は、AngularJSにクロスドメイン要求データを実装するための$ httpの使用について説明します。
Angularjs xmlhttprequest: $ httpは、リモートサーバーからデータを読み取るために使用されます
$ http.post(url、data、[config])。成功(function(){...}); $ http.get(url、[config])。success(function(){...}); $ http.get(url、[config])。成功(function(){...});1。$ http.jsonp [クロスドメインの実装]
1. callbackとコールバックの関数名を指定します。関数名がJSON_CALLBACKの場合、成功コールバック関数が呼び出されます。 JSON_CALLBACK大文字でなければなりません。
2。他のコールバック関数を指定しますが、ウィンドウの下で定義されているグローバル関数である必要があります。 callback URLに追加する必要があります。
2。$ http.get [クロスドメインの実装]
1.他のドメイン名の下でのアクセスを許可するようにサーバーを設定します
Response.setheader( "Access-Control-allow-Origin"、 "*"); //すべてのドメイン名がrespons.setheaderにアクセスできるようにする( "Access-Control-Allow-origin"、 "http://www.123.com"); // www.123.comにアクセスできるようにします
2。AngularJSサイドは$http.get()を使用します
3。$ http.post [クロスドメインの実装]
1.サーバー側の設定を設定して、他のドメイン名の下にアクセスできるように設定し、応答タイプと応答ヘッダー設定
Response.setheader( "Access-Control-allow-Origin"、 "*"); Restheader( "Access-Control-Allow-Methods"、 "post"); respons.setheader( "Access-control-allow-headers"、 "x-requested-with、content-type");
2。AngularJS側は$http.post()を使用し、リクエストヘッダー情報を同時に設定します
$ http.post( 'http://localhost/ajax/getallindustructioncategoty.pt'、{languagecolumn: 'name_eu'}、{'content-type': 'application/x-www-form-urlencoded'}).success(datas(data){$ scope.datustries;4。実装方法
クロスドメイン方法1 [ JSONP ]:
方法1:
$ http.jsonp( "http://localhost/sitesettings/getbadgeinfo.pt?jsonp = json_callback&siteid = 137bd406").success(function(data){...});方法2 [返品値、対応するコールバックメソッドを使用して受信する必要がありますが、 $scopeに配置するにはどうすればよいですか?]:
$ http.jsonp( "http://localhost/sitesettings/getbadgeinfo.pt?jsonp = badgeabc&siteid = 137bd406"); function badgeabc(data){...} public string execute()throws {string result = feal; Response.setheader( ""、 ""); sitehandleraction sitehandleraction =(sitehandleraction)beansfactory.getbean(sitehandleraction.class);バッジハンドラクションバッジハンドラクション=(バッジハンドラクション)beansfactory.getbean(badgehandleraction.class); if( ""。equals(siteid)|| siteid == null || stringutils.isblank( "jsonp")){result = fail; } else {site site = sitehandleraction.find(siteid); userbadgestatus userbadgestatus = badgehandleraction.getuserbadatatus(site.getid()); if(userbadgestatus!= null){result = "{/" /"+"+userbadatus.getStyle()+"、+"、+" lion//":+ userbadatatus.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();なしなし;}クロスドメイン法2 [ $http.get() ]:
function getadustrustercontroller($ scope、$ http){$ http.get( 'http://localhost/ajax/getallindustryctalcategoty.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': 'アプリケーション/x-ww-form-form-form-form-form-urlencodededed'} 'furtis-types $ scope.Industries = data;} // Java側は、Cross-Domain requests public String executeを要求します。 // respons.setheader( "Access-control-allow-methods"、 "post")にドメインを越えて要求することが許可されていますか); //許可された要求方法は、通常、取得、投稿、put、削除、オプションresponse.setheader( "Access-control-allow-headers"、 "x-requested-with、content-type")です。 //リクエストヘッダーを許可することが許可されていますクロスドメインはできます
sitehandleraction sitehandler =(sitehandleraction)beansfactory.getbean(sitehandleraction.class);リストlist = sitehandler.getallindustrustercategory(); //すべての分類コレクションjsonarray jsonarray = jsonarray.fromobject(list); // jsonling json = jsonarray.tostring()にリストをjsonstringに変換します。 // json stringに変換{printwriter write = respons.getWriter(); write.print(json); write.close();} catch(ioexception e){e.printstacktrace();} return none;}要約します
上記はこの記事に関するすべてです。この記事の内容が、Angularjsの使用を学ぶことを誰でも役立つことを願っています。