README
angular-http-cross-domain-request An angular service enabling cross-domain ajax requests using iframe from other domain and post messaging API.
Install
npm install angular-http-cross-domain-request
copy this files to your server's static files location:
$/tests/bridge_stub/bridge.html $/tests/bridge_stub/connect.utils.js $/tests/bridge_stub/connect.bridge.js
for example:
- TOMCAT Server - put these files in WEB-INF folder
- Express Server - put these files where js, html, css files are
Usage
angular
.module('myAppModule', [cdr])
.controller('MyAppController', MyAppController);
MyAppController.$inject = ['$scope', 'CdrService'];
function MyAppController($scope, CdrService){
var bridgeUrl = 'http://localhost:8081/bridge.html';
var apiUrl = 'http://localhost:8081/myApiFunction'
CdrService.init(bridgeUrl, function(iframe){
var urlParams = []
bodyData = null,
headers = {'Content-Type':'application/json', 'test': 'test'};
CdrService.get(apiUrl, urlParams, bodyData, headers, function(err, data){
if (err) //handle error
//handle response
});
});
});
API
CdrService.init(bridgePath, callback)
** this function is mandatory before other functions can be used
this function appends the remote domain's iframe and set post messaging listeners
CdrService.get(url, params, body, headers, cb){
this function makes a GET request to the remote API
- url: the remote API url
- params: data sent as request query params
- body: data sent in request body
- headers: json with all headers sent as request headers
CdrService.post(url, params, body, headers, cb){
this function makes a POST request to the remote API
- url: the remote API url
- params: data sent as request query params
- body: data sent in request body
- headers: json with all headers sent as request headers