Angularjs the simplest solution to cross-domain problem cases

Source: Internet
Author: User

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

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.