[英]Calling RESTful apis through ajax and angular js
If the URL that is to be hit has to be passed variables ie API.openweathermap.org/data/2.5/forecast/city?name=[random_city_name]&APPID=[key_value]
, 如果要传递的网址必须传递变量,即
API.openweathermap.org/data/2.5/forecast/city?name=[random_city_name]&APPID=[key_value]
,
then what is better to use ajax or angular js. 那么最好使用ajax或angular js。
If I am using ajax then how am I supposed to pass the variable? 如果我使用的是ajax,那么我应该如何传递变量? I am a newbie in this.
我是这个新手。 So, need your help.
因此,需要您的帮助。
Your url seems to have request parameters and assuming you are using angular1 您的网址似乎具有请求参数,并假设您使用的是angular1
For this, you can use 为此,您可以使用
$http({
method: 'GET',
url: url,
headers: {},
params : {}
})
Put your parameters as a map and $http will take care of creating an url. 将您的参数作为地图放置,$ http将负责创建一个URL。 Refer $http documentation here
在这里参考$ http文档
what is better to use ajax or angular js
有什么更好地使用ajax或angular js
You can't compare as AJAX
provides a way to communicate
(send requests and get responses) with the server asynchronously
and AngularJS
used AJAX
to extends the 2-way
data binding. 您无法比较,因为
AJAX
提供了一种与服务器asynchronously
communicate
(发送请求和获取响应)的方式,而AngularJS
使用AJAX
扩展了2-way
数据绑定。
To accomplish the above situation we can use Angular $http service. 为了完成上述情况,我们可以使用Angular $ http服务。
var baseUrl = API.openweathermap.org/data/2.5/forecast/city;
var method = 'GET';
var data = {};
var params = {
"name":cityName,
"APPID":key_value
};
$http({
method: method,
url: baseUrl,
params : params,
data : data
}).then(function mySucces(response) {
$scope.data = response.data;
}, function myError(response) {
$scope.data = response.statusText;
});
You can use angular $http service and pass your params like below.
var UserInfo = function() {
$scope.userID = "1111";
var req ={
"method":"GET",
"url": someURL + $scope.userID,
"withCredentials":true
};
$http(req).then(function(response) {
alert('success');
}, function(response) {
alert('error');
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.