作者:mobiledu2502869603 | 来源:互联网 | 2023-09-17 19:44
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.
我正在尝试从带有JSONP请求的API获取一些数据,但每次都得到404。我认为我的网址有误,但我可以在Chrome中手动点击网址并获得所需的响应。我的$ http.jsonp请求始终出现错误,错误回调中出现在我的console.log中的404。
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.
Chrome Dev Tools的网络面板显示了对我所期望的响应主体的请求的200响应,但是由于某种原因,它没有进入Angular。
2 个解决方案