0

I am using AngularJS $http for sending HTTP Get Request.

$http({
   method: 'get',
   url: 'http://cross-domain-url/api/v1/service1',
   params : {'key1' : 'value1'},
   data:'',
   headers : {'Accept': 'application/JSON', 
   'Content-Type' : 'application/JSON'},
   cache : false
}).then(function (response) {
   $waitDialog.hide();
   return response;
});

But this results in following error "Origin localhost is not allowed by Access-Control-Allow-Origin."

But when I am changing method to JSONP I am getting the correct response but that response is in XML. As content-type cannot be set using JSONP and that API by default use application/xml type. Is there any way I can request data from third party API that returns data in XML. ?

P.S: As the third party is controlled by someone else so I cant change default response type of data.

4

2 回答 2

0

另一种方法是使用$resorce

$http.defaults.useXDomain = true;
var Srv = $resource('http://cross-domain-url/api/v1/service1', {
    key1: '@key'
});

Srv.get({
    key: 'value1'
}, function(data) {
    ...
}, function(error) {
    ...
});
于 2013-08-06T00:45:55.890 回答
0

将方法更改为“JSONP”而不是“GET”或输入新键:值“dataType”:“JSONP”

在这里,我已经给出了代码......

$http({
   method: 'JSONP',
   url: 'http://cross-domain-url/api/v1/service1',
   params : {'key1' : 'value1'},
   data:'',
   headers : {'Accept': 'application/JSON', 
   'Content-Type' : 'application/JSON'},
   cache : false
}).then(function (response) {
   $waitDialog.hide();
   return response;
});
于 2016-04-30T06:59:18.197 回答