ANGULARJS Study Notes (i)-----JSONP cross-site access

Source: Internet
Author: User

1, download the Angular-seed-master-master package, unzip the new version of the Angularjs file into it.

2, make Frontmockup folder (front-end call), while making Backmockup folder (back-end call), using IIS to open 2 webserver (this step can be replaced in other ways).

3. Add the following HTTP headers to the Backmockup site

Access-control-allow-credentials:true

Access-control-allow-headers:origin,x-requested-with,content-type

Access-control-allow-methods:post,get,options

access-control-allow-origin:*

(This is used to turn on cross-domain access, which is added in IIS in the HTTP response header)

4, AngularJs cross-domain access using $resource JSONP technology, the code is as follows

. Service (' Jsonsource ', [' $resource ', function ($resource) {

Return $resource (' http://127.0.0.1:801/index.asp ', {}, {init:{method: ' JSONP ', Format: ' JSON ', Params:{callback: ' JSON _callback '},isarray:true}});

}])

Where the Init method is custom, because it needs to use method: ' JSONP ', so can not use GET, query and other methods, the array is set directly with IsArray.

Params:{callback: ' Json_callback ' is a key point, Json_callback is a system method that generates a self-growth ID to match the client with <%=request ("callback")% > Way to get, and output

Angular.callbacks._0 (

[{"Name": "Angular.callbacks._0", "journal_id": "539016f202b418c1e6000019"}])

The original form of this method is CALLBACK: ' Json_callback ', does not use the params, because the request can not be used to obtain parameters, do not know how to configure the server side, theoretically this option is more secure.

5, the data read call Jsonsource init method

function ($scope, Jsonsource) {

Jsonsource.init (function (result) {

$scope. Journals = result;

});

}

Loop output via ng-repeat= "journal in Journals"

6, cross-site access to complete the function, organized into compressed file angularjs-0.0-20140610-jsonp.zip.

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.