gpt4 book ai didi

javascript - angularJS 中是否有等于 getJSON 的方法。 [新手警报]

转载 作者:数据小太阳 更新时间:2023-10-29 05:01:19 26 4
gpt4 key购买 nike

我是 javascript、angularJS 和 JQuery 的新手,但我刚刚开始编写一个 angularJS 应用程序,我在其中使用 JQuery 从网络服务器获取 JSON,如下所示:

var obj = $.getJSON( "http://something.com/lol?query="+ $scope.searchString, function() {
$scope.items = obj.responseJSON.entries;
}

在angularJS中有没有等同于$.getJSON的方法?这样我就不必导入 JQuery 库了。

提前致谢,新手。

到目前为止,这是我的解决方案:

function InstantSearchController($scope, $http){
$scope.search = function() {
$http.jsonp("http://something.com/lol?query="+ $scope.searchString + "?json_callback=JSON_CALLBACK").success(
function(data, status) {
console.log(data);
}
);
}

但我收到错误消息:

Uncaught SyntaxError: Unexpected token :

这是为什么呢?我究竟做错了什么?

最佳答案

由于我从回答我的问题的人那里得到的帮助,我终于设法修复了它,我是这样做的:

app.controller('myController', function($scope, $http){
$scope.items = [];
$scope.search = function() {
$http({method: 'JSONP', url: "http://something.com/lol?callback=JSON_CALLBACK&query="+ $scope.searchString}).
success(function(data, status) {
$scope.items = data.entries;
}).
error(function(data, status) {
console.log(data || "Request failed");
});
};

希望这对以后遇到同样问题的人有帮助:D

关于javascript - angularJS 中是否有等于 getJSON 的方法。 [新手警报],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19356841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com