Javascript angularJS:http 请求配置 url 必须是字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36032159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
angularJS: http request configuration url must be a string
提问by Miha ?u?ter?i?
I have a function that takes an input from the front-end, and then concatinates that input into an 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:
我有一个从前端获取输入的函数,然后将该输入连接到我想从维基百科获取的 URL 中。由于我在使用 CORS 时遇到问题,因此我将 $http.get 实现为 JSONP,现在出现以下错误:
angular.js:13236 Error: [$http:badreq] 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"}
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% 7C原版"}
The thing is, that his error shows the concatinated url as a string?
问题是,他的错误将连接的 url 显示为字符串?
Can anybody point out what I'm doing wrong?
谁能指出我做错了什么?
This is the function I am calling:
这是我正在调用的函数:
//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 additional information, please let me know.
如果您需要其他信息,请告诉我。
回答by sp00m
$http({
method: 'JSONP',
url: url
}).then(function successCallback(response) {
// ok
}, function errorCallback(response) {
// ko
});
$http.get
is a shortcut methodfor $http({ method: 'GET' })
, and expects the URL as the first parameter.
$http.get
是一个快捷方法为$http({ method: 'GET' })
,并期望的URL作为第一个参数。
As you're using JSONP, you could also use the $http.jsonp
shortcut:
当您使用 JSONP 时,您还可以使用$http.jsonp
快捷方式:
$http.jsonp(url).then(function successCallback(response) {
// ok
}, function errorCallback(response) {
// ko
});