Http Get-Request和JSONP不工作
Http Get Request and JSONP not working
我使用AngularJS$http发送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;
});
但这导致了以下错误"访问控制允许起源不允许起源本地主机。"
但当我将方法更改为JSONP时,我得到了正确的响应,但该响应是XML的。由于不能使用JSONP设置内容类型,并且该API默认使用application/xml类型。是否有任何方法可以向返回XML数据的第三方API请求数据?
p.S:由于第三方由其他人控制,所以我无法更改数据的默认响应类型。
替代方案是使用$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) {
...
});
将方法更改为'JSONP'而不是'GET',或者放入一个新键:value"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;
});
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- Yelp API、OAuth和Angular与JSONP只工作一次
- 对 JSONP 请求的工作方式感到困惑
- Phonegap Android与JSONP不工作
- Http Get-Request和JSONP不工作
- Jsonp请求拒绝工作
- 首次尝试使用jsonp后,脚本无法工作
- 将angularjs与静态jsonp一起使用只能工作一次
- 不能让这个jQuery AJAX + JSONP工作.“没有& # 39;Access-Control-Allow-Or
- Firefox jquery ajax JSONP调用不工作
- JSONP回调不能从这个电影院url工作
- JSONP适配器Phonegap项目不工作
- JSONP通过getJson不工作
- Angular的$http.jsonp()方法只工作一次
- JSONP与Angularjs不能与TwitchTV api一起工作
- JSONP请求PHP页面不工作(跨域)
- JSONP Ajax调用不能并行工作