我正在尝试使用 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 只是示例。
最佳答案
来自ngDocs :
The name of the callback should be the string JSON_CALLBACK
Angular 将在内部用 angular.callbacks_{0-9a-z}
所以首先将您的代码更改为:
var url = 'http://muser:mpasswd@myredmine/redmine/issues.json?callback=JSON_CALLBACK';
$http.jsonp(url).succe...
玩这个fiddle似乎Redmine从 url 中剥离点 .
,从而使 angular.callbacks_0
变为未定义的 angularcallback_0
。
您可以在 github 上阅读更多内容,这里有一个问题。
可能的解决方案
1) 另一个用户在 SO here 上发布了一个 hack将回调的名称更改为自定义名称。
工作 fiddle根据@Todi-Adiatmo 的技巧。
2) 根据@dancras 在 github 上发布的问题上找到的另一个解决方案是通过调用 angular.callbacks_{..}
定义未定义的 angularcallbacks_{..}
然后删除全局 angularcallbacks_
.< br/>
您必须在 jsonp 调用之前立即使用它:
var c = $window.angular.callbacks.counter.toString(36);
$window['angularcallbacks_' + c] = function (data) {
$window.angular.callbacks['_' + c](data);
delete $window['angularcallbacks_' + c];
};
工作 fiddle根据@danca 的解决方案。
关于javascript - jsonp 在调用 Redmine API 时得到 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34317278/