【问题标题】:angularJS: http request configuration url must be a stringangularJS:http请求配置url必须是字符串
【发布时间】:2016-03-16 09:54:43
【问题描述】:

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

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

问题是,他的错误将连接的 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);
        });
};

如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:
    $http({
      method: 'JSONP',
      url: url
    }).then(function successCallback(response) {
      // ok
    }, function errorCallback(response) {
      // ko
    });
    

    $http.getshortcut method$http({ method: 'GET' }),并希望 URL 作为第一个参数。

    当您使用 JSONP 时,您也可以使用 $http.jsonp shortcut:

    $http.jsonp(url).then(function successCallback(response) {
      // ok
    }, function errorCallback(response) {
      // ko
    });
    

    【讨论】:

      猜你喜欢
      • 2016-11-15
      • 1970-01-01
      • 2017-05-18
      • 2021-12-16
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      • 1970-01-01
      相关资源
      最近更新 更多