【发布时间】:2014-07-27 04:19:15
【问题描述】:
我正在尝试通过 JSONP 请求从 API 获取一些数据,但每次都得到 404。我会假设我的 URL 是错误的,但我可以在 Chrome 中手动点击 URL 并获得所需的响应。我的$http.jsonp 请求总是出错,在错误回调中我的console.log 中显示404。
这是发出请求的代码:
$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);
});
}
};
Chrome 开发工具的网络面板显示了一个 200 响应请求以及我期望的响应正文,但无论出于何种原因,这都没有到达 Angular。
【问题讨论】:
-
@klode 我已经看到了。可能是 API 不支持 JSONP 请求。这可能吗?
-
我也有同样的问题。
标签: javascript angularjs jsonp