我试图通过JSONP请求从API获取一些数据,但每次都得到404。我认为我的网址有误,但我可以在Chrome中手动点击网址并获得所需的响应。我的$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。
答案 0 :(得分:5)
在与一些后端开发人员交谈后,看来我使用的API不支持JSONP。这就是我在该请求上获得404的原因。
答案 1 :(得分:1)
您可以在ajax请求之前提供window.console.log(siteNameUrl)的确切内容并提供结果吗?
也许尝试一个siteNameUrl.toString()以防万一它是一个解析问题
您似乎并没有要求完全相同的网址...
尝试使用siteNameUrl =" www.someapi / api"