For example, we can demonstrate cross-domain issues with two primary domains or a primary domain name + a two-level domain name.
- Client a.com
- Service-side B.Com or s.a.com
- Angularjs version V1.2.25
The preparation is done very well, the difference is that our editor is subline exposed, this general person I do not tell him drip.
Some people shout, this question is old already, what meaning do you propose now? Can you still turn the flowers away? Well, I can really write the flowers off. Let's go to the theater, this is the full version of the cross-domain instance.
Now let's look at how the client requests the data
Note Oh, our code is written under the a.com domain name
<! DOCTYPE html>var app = Angular.module (' app ',[]); App.controller (' Appctrl ', [' $scope ', ' $http ',function ($scope, $http) {$http({method: ' JSONP ',URL: ' Http://www.b.com/test.php?callback=JSON_CALLBACK ',}). Success (function(msg) {Console.log ( < Span style= "COLOR: #000000" > msg ); }); // or $http. Jsonp (' http:/ /www.b.com/test.php?callback=json_callback ' function (msg) {Console. Log (msg);}); }]); </script>
We see that this is a cross-domain request directly in the form of a JSONP, which is the same as in jquery for cross-domain requests. Note that our CALLBACK is fixed, that is, Json_callback, try not to make any changes
We look at the server B.Com in the test.php of the request data processing, here in native PHP way to do reference
Isset ($_get[' callback '): default_callback; Exit ($callBack. ' ('. Json_encode ($data). ');
Angularjs the simplest solution to cross-domain problem cases