我正在尝试从 angularjs 项目中使用 Redmine API。
我最终使用 jsonp 来解决 CORS 问题。
我在调用这个时收到 404:
var url = 'http://muser:mpasswd@myredmine/issues.json?callback=displayIssues';
$http({
method: 'JSONP',
url: url
}).
success(function (data, status) {
console.log("success");
console.log(data);
}).
error(function (data, status) {
console.log("Error: " + status);
});
...
function displayIssues(issues) {
console.log('displayIssues');
...
}
但是当我用 Postman 调用相同的 url 时,它可以工作。
为什么我会收到这个 404?
这是我的控制台:
GET http://muser:mpasswd@myredmine/issues.json?callback=displayIssues jsonpReq @ angular.js:8576(anonymous function) @ angular.js:8420sendReq @ angular.js:8291serverRequest @ angular.js:8025wrappedCallback @ angular.js:11498wrappedCallback @ angular.js:11498(anonymous function) @ angular.js:11584Scope.$eval @ angular.js:12608Scope.$digest @ angular.js:12420Scope.$apply @ angular.js:12712(anonymous function) @ angular.js:18980x.event.dispatch @ jquery-2.0.3.min.js:5y.handle @ jquery-2.0.3.min.js:5
authentication.service.js:100 Error: 404
Rest API 和 jsonp 都在 admin->setting->auth 中启用
我也试过:
var url = 'http://muser:mpasswd@myredmine/redmine/issues.json&callback=JSON_CALLBACK';
$http.jsonp(url, {
params: {
callback: 'JSON_CALLBACK',
format: 'json'
}
}).success(function (data) {
console.log('ok');
}).error(function (data) {
console.log('error: ' + JSON.stringify(data));
});
得到这个:
GET http://muser:mpasswd@myredmine/issues.json&callback=angular.callbacks._0?callback=JSON_CALLBACK&format=json jsonpReq @ angular.js:8576(anonymous function) @ angular.js:8420sendReq @ angular.js:8291serverRequest @ angular.js:8025wrappedCallback @ angular.js:11498wrappedCallback @ angular.js:11498(anonymous function) @ angular.js:11584Scope.$eval @ angular.js:12608Scope.$digest @ angular.js:12420Scope.$apply @ angular.js:12712(anonymous function) @ angular.js:18980x.event.dispatch @ jquery-2.0.3.min.js:5y.handle @ jquery-2.0.3.min.js:5
services.js:35 error: undefined
另外,当调用这个时:
var url = 'http://muser:mpasswd@myredmine/redmine/issues.json&callback=JSON_CALLBACK';
$http.jsonp(url).success(function (data) {
console.log('success');
}).error(function (error) {
console.log('error:: ' + error);
});
我犯了同样的错误
请考虑 muser、mpasswd 和 myredmine 只是示例。