Angularjs Solve Cross-domain problem cases (simple method) _angularjs

Source: Internet
Author: User

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.

<! DOCTYPE html>
 
 

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.