First of all, we do a little preparation, or you know what I mean by others do not understand, even if others understand that there is always someone do not understand, then you have to say, I mean this statement must be done, the answer is necessary, in order to better facilitate the understanding of everyone.
For example, we can demonstrate cross-domain problems with two primary domain names or a primary domain name plus a level two domain name.
Client a.com
Service-side B.Com or s.a.com
Angularjs version V1.2.25
The preparation work is very good, 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 keep the flowers open? Well, I can really write the flowers off. Let's go to the theater. The full version of the cross domain instance is being staged in this article.
Next, let's look at how the client requests the data.
Note Oh, our code is written under the a.com domain name.
We see that this is a cross-domain request directly in the form of JSONP, which operates in the same way as a cross-domain request in jquery. Note that our CALLBACK is fixed, that is, Json_callback, try not to make any changes
We look at the service-side B.Com in the test.php of the request data processing, here in the native PHP way to do the reference
$callBack = isset ($_get[' callBack '))? $_get[' callback ']: default_callback;
Exit ($callBack. ' ('. Json_encode ($data). ");
We'll come back and look at the results of the client Console.log record
To this, that is to say, our Cross-domain request is a success!
Finally, let's make a short summary and note the key points:
1. The parameters appended by the client to the Cross-domain request are? Callback=json_callback, the value of the parameter callback is specified as Json_callback, note is uppercase, json_callback do not make any changes, It feels like it's a big hole, huh? Callback value slightly changes, the client needs to define the global callback function, and also how to pass to $scope processing? In order to avoid unnecessary trouble, here's the suggestion.
2. Again look at the service side, the server needs to specify $callback = $_get[' CallBack '; receive CallBack, and you will also find that received CallBack is not our client write? CallBack value, client specified Json_ Callback should be to trigger the internal mechanism of the ANGULARJS.