7

我正在尝试从 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 只是示例。

4

2 回答 2

5

ngDocs

回调的名称应该是字符串 JSON_CALLBACK

Angular 会在内部用angular.callbacks_{0-9a-z}

因此,首先将您的代码更改为:

var url = 'http://muser:mpasswd@myredmine/redmine/issues.json?callback=JSON_CALLBACK';
$http.jsonp(url).succe...

玩这个小提琴似乎Redmine从url中删除了点.,从而使其angular.callbacks_0未定义angularcallback_0你可以在github
上阅读更多内容,这里有一个问题。

可能的解决方案

1)另一个用户在 SO 上发布了一个 hack 将回调的名称更改为自定义名称。

根据@Todi-Adiatmo 的技巧工作小提琴。

2)根据@dancras在githubangularcallbacks_{..}上发布的问题上找到的另一个解决方案是通过调用定义 undefined ,angular.callbacks_{..}然后删除 global angularcallbacks_
您必须在 jsonp 调用之前立即使用它:

var c = $window.angular.callbacks.counter.toString(36);

$window['angularcallbacks_' + c] = function (data) {
    $window.angular.callbacks['_' + c](data);
    delete $window['angularcallbacks_' + c];
};

根据@danca 的解决方案工作小提琴。

于 2015-12-28T21:55:54.413 回答
0

我看到您的控制台显示以下内容:-

获取http://muser:mpasswd@myredmine/issues.json?callback=displayIssues

从技术上讲,这不是 HTTP 中的有效 URL。用户名和密码不应该在 GET 调用中,它们应该在标题中,根据。

https://en.wikipedia.org/wiki/Basic_access_authentication

在谷歌上快速搜索发现了这个https://docs.angularjs.org/api/ng/service/ $http 和 angularjs 基本身份验证标头

我已经好几年没看过 redmine 了,它可以简单地使用前面的答案指定的选项来处理不可靠的 HTTP,但是如果这不起作用,请检查请求是否真的正确地到达了 Redmine

于 2016-01-04T14:45:19.957 回答