AngularJS: http request configuration url must be a string

I have a function that takes input from the front-end and then concatenates this input to the URL that I want to get from Wikipedia. Since I had problems with CORS, I implemented my $ http.get as JSONP, and now I get the following error:

angular.js: 13236 Error: [$ http: badreq] The Http request configuration URL must be a string. Received: {"method": "JSONP", "url": " https://en.wikipedia.org/w/api.php?action=query&format=json&uselang=user&prop=extracts%7Cpageimages&titles=Maya+Angelou&piprop=name% 7Coriginal "}

Is the fact that his error shows a concatenated url as a string?

Can anyone point out what I'm doing wrong?

This is the function I call:

//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);
        });
};

If you need more information, please let me know.

+4
source share
1 answer
$http({
  method: 'JSONP',
  url: url
}).then(function successCallback(response) {
  // ok
}, function errorCallback(response) {
  // ko
});

$http.getis a shortcut method for $http({ method: 'GET' })and expects the url to become the first parameter.

When you use JSONP, you can also use a $http.jsonpshortcut :

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

All Articles