JSONP请求在Angular应用程序中提供404 [英] JSONP request gives 404 in Angular app
问题描述
我正在尝试从带有JSONP请求的API获取一些数据,但每次都得到404。我认为我的网址有误,但我可以在Chrome中手动点击网址并获得所需的响应。我的 $ http.jsonp
请求错误,并在错误回调中出现在我的 console.log
中的404。这是发出请求的代码:
$ scope.fetchDefaultLabel = function(){
//当端点字段模糊时,此代码最终将标签设置为站点名称。
//目前无法正常工作。
if($ scope.endpoint){
var baseUrl = $ scope.endpoint.split('/')[2];
var siteNameUrl ='http://'+ baseUrl +'/ api / sititame.json?callback = JSON_CALLBACK';
console.log(siteNameUrl);
$ http.jsonp(siteNameUrl)
.success(函数(数据,状态,标题,配置){
$ scope.label = data;
})
.error(函数(data,status,headers,config){
console.log('Data:'+ data);
console.log('Status:'+ status);
console.log('标题:'+标题);
console.log('配置:'+配置);
});
}
};
Chrome开发工具的网络面板显示响应请求的200响应,我希望响应正文,但无论出于何种原因,这都没有达到Angular。
在与一些后端开发人员交谈后,看来我是API使用不支持JSONP。这就是我在该请求上获得404的原因。
I'm trying to get some data from an API with a JSONP request, but I get a 404 every time. I would assume my URL is wrong, but I can hit the URL manually in Chrome and get the desired response. My $http.jsonp
request always errors with a 404 showing up in my console.log
in the error callback.
Here's the code that makes the request:
$scope.fetchDefaultLabel = function () {
// This code will eventually set the label to the site name when the endpoint field blurs.
// Currently not working properly.
if ($scope.endpoint) {
var baseUrl = $scope.endpoint.split('/')[2];
var siteNameUrl = 'http://' + baseUrl + '/api/sitename.json?callback=JSON_CALLBACK';
console.log(siteNameUrl);
$http.jsonp(siteNameUrl)
.success(function(data, status, headers, config) {
$scope.label = data;
})
.error(function(data, status, headers, config) {
console.log('Data: ' + data);
console.log('Status: ' + status);
console.log('Headers: ' + headers);
console.log('Config: ' + config);
});
}
};
Network panel of Chrome Dev Tools shows a 200 response for the request with the response body I would expect, but that's not getting to Angular for whatever reason.
After talking with some back-end developers, it appears the API I am using does not support JSONP. This is the reason I was getting a 404 on that request.
这篇关于JSONP请求在Angular应用程序中提供404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!