调用 Redmine 时 jsonp 得到 404 API

jsonp getting 404 when calling Redmine API

我正在尝试使用 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 it seems that Redmine 从 url 中去除点 .,从而使 angular.callbacks_0 变为 angularcallback_0,这是未定义的。
您可以在 github 上阅读更多内容,这里有一个问题。

可能的解决方案

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

根据@Todi-Adiatmo 的技巧fiddle 工作。

2) 根据@dancras 在 github 上发布的问题上找到的另一种解决方案是通过调用 [=] 来定义未定义的 angularcallbacks_{..} 17=] 然后删除全局 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 的解决方案工作fiddle

我看到你的控制台显示这个:-

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

从技术上讲,这在 HTTP 中不是有效的 URL。根据

,用户名和密码不应该在 GET 调用中,它们应该在 header 中

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

在 google 上快速搜索找到了这个 https://docs.angularjs.org/api/ng/service/$http 和 angularjs basic authentication headers

我已经很多年没看过 redmine 了,它可能仅使用先前答案指定的选项就可以很好地处理狡猾的 HTTP,但如果那不起作用,请检查请求是否真的正确到达 Redmine