我有一个函数,它做一个http POST请求。代码如下所示。这很好。
$http({
url: user.update_path,
method: "POST",
data: {user_id: user.id, draft: true}
});
我有另一个函数http GET,我想发送数据到该请求。但是我在get中没有这个选项。
$http({
url: user.details_path,
method: "GET",
data: {user_id: user.id}
});
http的语法。得到的是
得到(url,配置)
下面是在ASP中使用angular.js的HTTP GET请求的完整示例。NET MVC:
控制器:
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);
}
}
观点:
<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>