javascript angularJS 中是否有一种方法等于 getJSON。【新手提醒】
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19356841/
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
Is there a method in angularJS thats equal to getJSON. [Newbie alert]
提问by Erex
I'm newbie at javascript, angularJS and JQuery, but I have just started programming a angularJS app where i use JQuery to get a JSON from a webserver like this:
我是 javascript、angularJS 和 JQuery 的新手,但我刚刚开始编写 angularJS 应用程序,我使用 JQuery 从这样的网络服务器获取 JSON:
var obj = $.getJSON( "http://something.com/lol?query="+ $scope.searchString, function() {
$scope.items = obj.responseJSON.entries;
}
Is there a method equal to $.getJSON in angularJS? So that I don't have to import the JQuery library.
angularJS 中是否有等于 $.getJSON 的方法?这样我就不必导入 JQuery 库了。
Thanks in advance, newbie.
在此先感谢,新手。
This is my solution so far:
到目前为止,这是我的解决方案:
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);
}
);
}
but I'm getting the error msg:
但我收到错误消息:
Uncaught SyntaxError: Unexpected token :
未捕获的语法错误:意外标记:
why is this? what am I doing wrong? }
为什么是这样?我究竟做错了什么?}
回答by Erex
Because of the help i got from people answering my question I finally managed to fix it, and i did it like this:
由于我从回答我的问题的人那里得到的帮助,我终于设法解决了这个问题,我是这样做的:
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");
});
};
Hope this helps anyone who has the same problem in the future :D
希望这可以帮助将来遇到同样问题的任何人:D
回答by Darin Dimitrov
回答by Vikash Pathak
You may use JSONP requests with $http.jsonp
您可以将 JSONP 请求与 $http.jsonp 一起使用
回答by Vianney Dupoy de Guitard
There is an alternative in AngularJS called $http
, you can find more here.
For instance :
在 AngularJS 中有一个替代方案$http
,您可以在此处找到更多信息。例如 :
$http({method: 'JSONP', url: 'http://domain.com/page?json_callback=JSON_CALLBACK'}).success(
function(data, status) {
// your stuff.
}
);
Or even shorter :
甚至更短:
$http.jsonp('http://domain.com/page?json_callback=JSON_CALLBACK').success(
function(data, status) {
// your stuff.
}
);
JSONP (JSON Padding) allows you to get JSON data from another domain. However, the data you get should not be plain JSON, but rather a Javascript file like this :
JSONP(JSON Padding)允许您从另一个域获取 JSON 数据。但是,您获得的数据不应该是纯 JSON,而应该是这样的 Javascript 文件:
JSON_CALLBACK([
{"name": "apple", "color": "red"},
{"name": "banana", "color": "yellow"}
]);
If your JSON data you need comes from the same domain, you do not need JSONP.
如果您需要的 JSON 数据来自同一个域,则不需要 JSONP。
回答by Lauri Elias
function ListProdcutsCtrl($scope, $http) {
var request = {'searchString' : 'apple'};
$http.get('/api/products', request).success(function(response) {
$scope.products_table_data = response.products;
});
回答by Prateek Shankar
JSONP is used to overcome the cross-domain restriction of the AJAX URL calls.
JSONP 用于克服 AJAX URL 调用的跨域限制。
With AngularJS (v1.5), you can use this code to send cross domain requests:
使用 AngularJS (v1.5),您可以使用此代码发送跨域请求:
$http.jsonp(baseurl+'?token=assume_jwt_token'+encoding+type + "&callback=JSON_CALLBACK")
The Syntax for AngularJS JSONP request is :
AngularJS JSONP 请求的语法是:
$http.jsonp(url, [config]);
where url is "string" type representing Relative or absolute URL specifying the destination of the request. The name of the callback should be the string JSON_CALLBACK, and [config] is Optional configuration object.
其中 url 是“字符串”类型,表示指定请求目的地的相对或绝对 URL。回调的名称应该是字符串 JSON_CALLBACK,[config] 是可选的配置对象。