I have a function which does a http POST request. The code is specified below. This works fine.
我有一个处理http POST请求的函数。代码在下面指定。这是很好。
$http({
url: user.update_path,
method: "POST",
data: {user_id: user.id, draft: true}
});
I have another function for http GET and I want to send data to that request. But I don't have that option in get.
我有另一个http GET函数,我想把数据发送给那个请求。但是我没有这个选项。
$http({
url: user.details_path,
method: "GET",
data: {user_id: user.id}
});
The syntax for http.get
is
http的语法。得到的是
get(url, config)
get(url、配置)
Can someone help me with this?
有人能帮我吗?
7 个解决方案
#1
885
An HTTP GET request can't contain data to be posted to the server. However, you can add a query string to the request.
HTTP GET请求不能包含要发送到服务器的数据。但是,您可以向请求添加查询字符串。
angular.http provides an option for it called params
.
角。http提供了一个名为params的选项。
$http({
url: user.details_path,
method: "GET",
params: {user_id: user.id}
});
See: http://docs.angularjs.org/api/ng.$http#get and https://docs.angularjs.org/api/ng/service/$http#usage (shows the params
param)
参见:http://docs.angularjs.org/api/ng $http#get和https://docs.angularjs.org/api/ng/service/$http#用法(显示params param)
#2
490
You can pass params directly to $http.get()
The following works fine
您可以直接将params传递给$http.get(),下面的工作很好。
$http.get(user.details_path, {
params: { user_id: user.id }
});
#3
37
Starting from AngularJS v1.4.8, you can use get(url, config)
as follows:
从AngularJS v1.4.8开始,您可以使用get(url, config)如下:
var data = {
user_id:user.id
};
var config = {
params: data,
headers : {'Accept' : 'application/json'}
};
$http.get(user.details_path, config).then(function(response) {
// process response here..
}, function(response) {
});
#4
31
Solution for those who are interested in sending params and headers in GET request
对于在GET请求中发送params和header感兴趣的人的解决方案
$http.get('https://www.your-website.com/api/users.json', {
params: {page: 1, limit: 100, sort: 'name', direction: 'desc'},
headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
}
)
.then(function(response) {
// Request completed successfully
}, function(x) {
// Request error
});
Complete service example will look like this
完整的服务示例如下所示
var mainApp = angular.module("mainApp", []);
mainApp.service('UserService', function($http, $q){
this.getUsers = function(page = 1, limit = 100, sort = 'id', direction = 'desc') {
var dfrd = $q.defer();
$http.get('https://www.your-website.com/api/users.json',
{
params:{page: page, limit: limit, sort: sort, direction: direction},
headers: {Authorization: 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
}
)
.then(function(response) {
if ( response.data.success == true ) {
} else {
}
}, function(x) {
dfrd.reject(true);
});
return dfrd.promise;
}
});
#5
4
You can even simply add the parameters to the end of the url:
您甚至可以简单地将参数添加到url的末尾:
$http.get('path/to/script.php?param=hello').success(function(data) {
alert(data);
});
Paired with script.php:
搭配script.php:
<? var_dump($_GET); ?>
Resulting in the following javascript alert:
导致以下javascript警告:
array(1) {
["param"]=>
string(4) "hello"
}
#6
1
For sending get request with parameter i use
对于发送带有参数的get请求,我使用
$http.get('urlPartOne\\'+parameter+'\\urlPartTwo')
By this you can use your own url string
您可以使用自己的url字符串。
#7
1
Here's a complete example of an HTTP GET request with parameters using angular.js in ASP.NET MVC:
这里有一个使用角度参数的HTTP GET请求的完整示例。js在ASP。NET MVC:
CONTROLLER:
控制器:
public class AngularController : Controller
{
public JsonResult GetFullName(string name, string surname)
{
System.Diagnostics.Debugger.Break();
return Json(new { fullName = String.Format("{0} {1}",name,surname) }, JsonRequestBehavior.AllowGet);
}
}
VIEW:
观点:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script type="text/javascript">
var myApp = angular.module("app", []);
myApp.controller('controller', function ($scope, $http) {
$scope.GetFullName = function (employee) {
//The url is as follows - ControllerName/ActionName?name=nameValue&surname=surnameValue
$http.get("/Angular/GetFullName?name=" + $scope.name + "&surname=" + $scope.surname).
success(function (data, status, headers, config) {
alert('Your full name is - ' + data.fullName);
}).
error(function (data, status, headers, config) {
alert("An error occurred during the AJAX request");
});
}
});
</script>
<div ng-app="app" ng-controller="controller">
<input type="text" ng-model="name" />
<input type="text" ng-model="surname" />
<input type="button" ng-click="GetFullName()" value="Get Full Name" />
</div>
#1
885
An HTTP GET request can't contain data to be posted to the server. However, you can add a query string to the request.
HTTP GET请求不能包含要发送到服务器的数据。但是,您可以向请求添加查询字符串。
angular.http provides an option for it called params
.
角。http提供了一个名为params的选项。
$http({
url: user.details_path,
method: "GET",
params: {user_id: user.id}
});
See: http://docs.angularjs.org/api/ng.$http#get and https://docs.angularjs.org/api/ng/service/$http#usage (shows the params
param)
参见:http://docs.angularjs.org/api/ng $http#get和https://docs.angularjs.org/api/ng/service/$http#用法(显示params param)
#2
490
You can pass params directly to $http.get()
The following works fine
您可以直接将params传递给$http.get(),下面的工作很好。
$http.get(user.details_path, {
params: { user_id: user.id }
});
#3
37
Starting from AngularJS v1.4.8, you can use get(url, config)
as follows:
从AngularJS v1.4.8开始,您可以使用get(url, config)如下:
var data = {
user_id:user.id
};
var config = {
params: data,
headers : {'Accept' : 'application/json'}
};
$http.get(user.details_path, config).then(function(response) {
// process response here..
}, function(response) {
});
#4
31
Solution for those who are interested in sending params and headers in GET request
对于在GET请求中发送params和header感兴趣的人的解决方案
$http.get('https://www.your-website.com/api/users.json', {
params: {page: 1, limit: 100, sort: 'name', direction: 'desc'},
headers: {'Authorization': 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
}
)
.then(function(response) {
// Request completed successfully
}, function(x) {
// Request error
});
Complete service example will look like this
完整的服务示例如下所示
var mainApp = angular.module("mainApp", []);
mainApp.service('UserService', function($http, $q){
this.getUsers = function(page = 1, limit = 100, sort = 'id', direction = 'desc') {
var dfrd = $q.defer();
$http.get('https://www.your-website.com/api/users.json',
{
params:{page: page, limit: limit, sort: sort, direction: direction},
headers: {Authorization: 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='}
}
)
.then(function(response) {
if ( response.data.success == true ) {
} else {
}
}, function(x) {
dfrd.reject(true);
});
return dfrd.promise;
}
});
#5
4
You can even simply add the parameters to the end of the url:
您甚至可以简单地将参数添加到url的末尾:
$http.get('path/to/script.php?param=hello').success(function(data) {
alert(data);
});
Paired with script.php:
搭配script.php:
<? var_dump($_GET); ?>
Resulting in the following javascript alert:
导致以下javascript警告:
array(1) {
["param"]=>
string(4) "hello"
}
#6
1
For sending get request with parameter i use
对于发送带有参数的get请求,我使用
$http.get('urlPartOne\\'+parameter+'\\urlPartTwo')
By this you can use your own url string
您可以使用自己的url字符串。
#7
1
Here's a complete example of an HTTP GET request with parameters using angular.js in ASP.NET MVC:
这里有一个使用角度参数的HTTP GET请求的完整示例。js在ASP。NET MVC:
CONTROLLER:
控制器:
public class AngularController : Controller
{
public JsonResult GetFullName(string name, string surname)
{
System.Diagnostics.Debugger.Break();
return Json(new { fullName = String.Format("{0} {1}",name,surname) }, JsonRequestBehavior.AllowGet);
}
}
VIEW:
观点:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script type="text/javascript">
var myApp = angular.module("app", []);
myApp.controller('controller', function ($scope, $http) {
$scope.GetFullName = function (employee) {
//The url is as follows - ControllerName/ActionName?name=nameValue&surname=surnameValue
$http.get("/Angular/GetFullName?name=" + $scope.name + "&surname=" + $scope.surname).
success(function (data, status, headers, config) {
alert('Your full name is - ' + data.fullName);
}).
error(function (data, status, headers, config) {
alert("An error occurred during the AJAX request");
});
}
});
</script>
<div ng-app="app" ng-controller="controller">
<input type="text" ng-model="name" />
<input type="text" ng-model="surname" />
<input type="button" ng-click="GetFullName()" value="Get Full Name" />
</div>