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