Ere*_*rex 6 javascript jquery syntax-error getjson angularjs
我是javascript,angularJS和JQuery的新手,但我刚刚开始编写一个angularJS应用程序,我使用JQuery从这样的网络服务器获取JSON:
var obj = $.getJSON( "http://something.com/lol?query="+ $scope.searchString, function() {
$scope.items = obj.responseJSON.entries;
}
Run Code Online (Sandbox Code Playgroud)
在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);
}
);
}
Run Code Online (Sandbox Code Playgroud)
但我收到错误信息:
未捕获的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");
});
};
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助将来遇到同样问题的人:D
| 归档时间: |
|
| 查看次数: |
14863 次 |
| 最近记录: |