Cross-domain problem of Angularjs's $http

Source: Internet
Author: User

Understanding the nature of Angularjs's $http approach is the same as the jquery Ajax approach, began to worry about cross-domain issues, the previous development work tense, encountered also put the cross-domain issues down, after all, do the traditional web development, the backend provides the interface and front end will be published on a server, Now the project is big, naturally encountered some team is only responsible for publishing API, and our team can only call, cross-domain is certainly not skipped steps.

Did not expect such an understanding incredibly have so many cross-domain method, also do not know feasible infeasible! First Mark, then.

First, $http. Jsonp "Implementing Cross-Domain"

1. Specify the callback and callback function names, and when the function name is Json_callback, the success callback function is called and the Json_callback must be all uppercase.
2. Specify a different callback function, but it must be a global function defined under window. Callback must be added to the URL.

Realize:

Method One:

$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 Two "Returns a value that needs to be received using the corresponding callback method, but how to place the $scope??? 】:

$http. Jsonp ("http://localhost/sitesettings/getBadgeInfo.pt?jsonp=badgeabcbadgeabc(data) {...}
Ii. $http. Get "implement cross-domain"

1. Server-side settings allow access under other domain names

Response.setheader ("Access-control-allow-origin", "*"); Allow all domain names to access Response.setheader ("Access-control-allow-origin", "http://www.123.com"); Allow www.123.com access

2. Angularjs end using $http.get ()

Realize:

$http. Get (' Http://localhost/ajax/getAllIndustryCategoty.pt?languageColumn=name_eu '). Success (function (data) {$ scope.industries = data;});
Iii. $http. Post "Implementing Cross-Domain"

1. Server-side settings allow access under other domain names, and response type, response header settings

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 End uses $http.post () to set the request header information

Realize:

$http. Post (' http://localhost/ajax/getAllIndustryCategoty.pt ', {languagecolumn: ' Name_eu '},{' content-type ': ' Application/x-www-form-urlencoded '}). Success (data) {$scope. Industries = data;});

Cross-domain problem of Angularjs's $http

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.