Introduction to angular. js cross-origin post solution, Introduction to angular. js

Source: Internet
Author: User

Introduction to angular. js cross-origin post solution, Introduction to angular. js

Cross-origin: problems frequently encountered in front-end development. AngularJS implements cross-origin methods similar to Ajax and uses CORS mechanisms.

The following describes how to use $ http in AngularJS to implement cross-origin request data.

AngularJS XMLHttpRequest: $ http is used to read data from a remote server.

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

1. $ http. jsonp [implement cross-origin]

1. Specify the callback and callback function names. When the function name is JSON_CALLBACK, the success callback function is called. JSON_CALLBACK must be in uppercase.

2. Specify other callback functions, but must be global functions defined in window. Callback must be added to the url.

Ii. $ http. get [implement cross-origin]

1. Allow access under other domain names on the server side

Response. setHeader ("Access-Control-Allow-Origin", "*"); // Allow all domain names to Access response. setHeader ("Access-Control-Allow-Origin", "http://www.123.com"); // allows Access from www.123.com

2. AngularJS end uses $ http. get ()

Iii. $ http. post [implement cross-origin]

1. Allow access under other domain names on the server side, and set the response type and response header.

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 uses $ http. post () and sets request header information.

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

IV. Implementation Methods

Cross-origin method 1 [JSONP ]:

Method 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.

Method 2 [return value, which must be received using the corresponding callback method, but how to place it in $ 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;}

Cross-origin method 2 [$ http. get ()]:

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

Cross-origin method 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 supports cross-origin request public String execute () {response. setHeader ("Access-Control-Allow-Origin", "*"); // The URLs allowed for cross-Origin requests to response. setHeader ("Access-Control-Allow-Methods", "POST"); // allowed request Methods, usually GET, POST, PUT, DELETE, OPTIONS response. setHeader ("Access-Control-Allow-Headers", "x-requested-with, content-type"); // request Headers allowed for cross-origin SiteHandlerAction SiteHandler = (SiteHandlerAction) beansFactory. getBean (SiteHandlerAction. class); List list = SiteHandler. getAllIndustryCategory (); // all classification sets JSONArray jsonArray = JSONArray. fromObject (list); // convert list to json String json = jsonArray. toString (); // convert to json string try {PrintWriter write = response. getWriter (); write. print (json); write. close ();} catch (IOException e) {e. printStackTrace ();} return NONE ;}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.