AngularJs Case Study on solving cross-origin problems (simple method), angularjs
First, let's make some preparations. Otherwise, you will understand what I mean and what others will not understand. Even if someone else understands it, then someone will not understand it, I mean, this description must be done, and the answer is required, so that you can better understand it.
We can use two primary domain names or one primary domain name + one secondary domain name as an example to demonstrate cross-origin problems.
Client a.com
Server B .com or s.a.com
AngularJs V1.2.25
If the preparation work is well done, we just need to expose our editor as subline. I will not tell him this ordinary person.
Some people have been clamoring for a long time. What is the significance of your proposal? Why can't you just hide the flowers? Well, I can write the flowers. Let's take a look at the full cross-origin instance.
Next, let's first look at how the client requests data.
Note: Our code is written under the.com domain name.
<! DOCTYPE html>
We can see that the cross-origin request is directly in the form of jsonp, and its operations are the same as the Cross-origin request method in jquery. Note that our callback is fixed, namely, JSON_CALLBACK. Do not make any changes as far as possible.
Let's look at how test. php In server B .com processes the request data. Here we will use the native php Method for reference.
$callBack = isset($_GET['callback']) ? $_GET['callback'] : DEFAULT_CALLBACK;exit($callBack.'('.json_encode($data).')');
Let's take a look at the client console. log record results.
This means that our cross-origin request is successful!
Finally, let's make a summary. Pay attention to the following points:
1. What are the parameters appended to the url of the Client Cross-origin request? Callback = JSON_CALLBACK. The value of the callback parameter is specified as JSON_CALLBACK. Note that it is in uppercase, that is, JSON_CALLBACK should not be changed. It is a big pitfall ,? When the callback value is slightly changed, the client needs to define the callback function globally, and how can it be passed to $ scope for processing? In order to avoid unnecessary troubles, we suggest doing so here.
2. Check the server again. The server needs to specify $ callBack =$ _ GET ['callback']; To receive callback, and you will find that the received callback is not written by our client? The value of callback. The client specifies JSON_CALLBACK to trigger the internal mechanism of angularJs.