angularJS:http请求配置url必须是一个字符串

Mih*_*šič 5 javascript angularjs

我有一个从前端获取输入的函数,然后将该输入合并到我想从维基百科获取的URL中.由于我遇到CORS问题,我将$ http.get实现为JSONP,现在我收到以下错误:

angular.js:13236错误:[$ http:badreq] Http请求配置url必须是一个字符串.收到:{"method":"JSONP","url":" https://en.wikipedia.org/w/api.php?action=query&format=json&uselang=user&prop=extracts%7Cpageimages&titles=Maya+Angelou&piprop=name% 7原住民 "}

问题是,他的错误显示连接的URL是一个字符串?

谁能指出我做错了什么?

这是我打电话的功能:

//function to get author info from wikipedia
$scope.getAuthorInfo = function(author) {
    //remove whitespace from author
    author = author.replace(/\s/g, '+');
    //concat the get URL
    var url = 'https://en.wikipedia.org/w/api.php?action=query&format=json&uselang=user&prop=extracts%7Cpageimages&titles=' +
        author + '&piprop=name%7Coriginal';
    //get author info from wikipedia    
    $http.get({
            method: 'JSONP',
            url: url
        })
        .then(function successCallback(response) {
            $scope.author = response.data;
            //for every result from wikipedia, trust the extract as html
            for (var x in $scope.author.query.pages) {
                $scope.author.query.pages[x].extract = $sce.trustAsHtml($scope.author.query.pages[x].extract);
            }

        }, function errorCallback(response) {
            console.log(response);
        });
};
Run Code Online (Sandbox Code Playgroud)

如果您需要其他信息,请告诉我们.

sp0*_*00m 11

$http({
  method: 'JSONP',
  url: url
}).then(function successCallback(response) {
  // ok
}, function errorCallback(response) {
  // ko
});
Run Code Online (Sandbox Code Playgroud)

$http.get是一个快捷方法$http({ method: 'GET' }),并期望的URL作为第一个参数.

当您使用JSONP时,您还可以使用$http.jsonp快捷方式:

$http.jsonp(url).then(function successCallback(response) {
  // ok
}, function errorCallback(response) {
  // ko
});
Run Code Online (Sandbox Code Playgroud)