First we do some preparation, or you understand what I mean people do not understand, even if others understand that people do not understand, then you have to say, I mean this statement must be done, the answer is necessary, in order to better facilitate everyone understand it.
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>varApp = Angular.module (' app ', []); App. Controller (' Appctrl ', [' $scope ',function($scope) { $http({method: ' JSONP ',URL: ' Http://www.b.com/test.php?callback=JSON_CALLBACK ', }). Success (function(msg) {Console.Log(data); }); //or $http. JSONP (' Http://www.b.com/test.php?callback=JSON_CALLBACK ') . Success (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
$callBack isset ($_get$_get[' Callback ']: default_callback; Exit ($callBack. ' ('. Json_encode ($data). ');
We'll come back and look at the results of the client Console.log record
? [Considering the current domestic web site most of the collection of articles very frequently, not to mention the original source, the original author would like to see the original text, in case there are any problems can not update all articles, to avoid misleading! ]
Continue Reading
Angularjs the simplest solution to cross-domain problem cases