angularJS中是否有一个等于getJSON的方法。

我是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); } ); } 

但我收到错误消息:

未捕获的SyntaxError:意外的令牌:

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

由于我从人们回答我的问题得到的帮助,我终于设法解决它,我这样做:

 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

您可以使用$http在Angular中发送AJAX请求。

您可以使用$ http.jsonp的JSONP请求

https://docs.angularjs.org/api/ng/service/$http#jsonp

 function ListProdcutsCtrl($scope, $http) { var request = {'searchString' : 'apple'}; $http.get('/api/products', request).success(function(response) { $scope.products_table_data = response.products; }); 

AngularJS中有一个名为$http的替代品,你可以在这里找到更多 。 例如 :

 $http({method: 'JSONP', url: 'http://domain.com/page?json_callback=JSON_CALLBACK'}).success( function(data, status) { // your stuff. } ); 

甚至更短:

 $http.jsonp('http://domain.com/page?json_callback=JSON_CALLBACK').success( function(data, status) { // your stuff. } ); 

JSONP(JSON Padding)允许您从另一个域获取JSON数据。 但是,您获得的数据不应该是纯JSON,而应该是这样的Javascript文件:

 JSON_CALLBACK([ {"name": "apple", "color": "red"}, {"name": "banana", "color": "yellow"} ]); 

如果您需要的JSON数据来自同一个域,则不需要JSONP。

JSONP用于克服AJAX URL调用的跨域限制。

使用AngularJS(v1.5),您可以使用此代码发送跨域请求:

 $http.jsonp(baseurl+'?token=assume_jwt_token'+encoding+type + "&callback=JSON_CALLBACK") 

AngularJS JSONP请求的语法是:

 $http.jsonp(url, [config]); 

其中url是“string”类型,表示指定请求目标的相对或绝对URL。 回调的名称应为字符串JSON_CALLBACK,[config]为可选配置对象。