AngularJS发送异步Get/Post请求方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了AngularJS发送异步Get/Post请求方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2916字,纯文字阅读大概需要5分钟。
内容图文
![AngularJS发送异步Get/Post请求方法](/upload/InfoBanner/zyjiaocheng/337/08320e89658349dfb017568f8cf155c7.jpg)
1、在页面中加入AngularJS并为页面绑定ng-app 和 ng-controller
<body ng-app="MyApp" ng-controller="MyCtrl" > ... <script src="js/angular.min.js"></script> <script src="js/sbt.js"></script>
2、添加必要的控件并绑定相应的事件
get:<input type="text" ng-model="param">{{param}} <br> post: <input type="text" ng-model="user.name"><input type="text" ng-model="user.password"><br> <button ng-click="get()">Get</button> <button ng-click="post()">Post</button>
3、在JS脚本中发送进行Get/Post请求
get
$scope.get = function () { $http.get("/get", {params: {param: $scope.param}}) .success(function (data, header, config, status) { console.log(data); }) .error(function (data, header, config, status) { console.log(data); }) ; }
get 将参数放在URL中
$scope.get = function () { $http.get("/get?param="+$scope.param) .success(function (data, header, config, status) { console.log(data); }) .error(function (data, header, config, status) { console.log(data); }) ; }
post
$scope.post = function () { $http.post("/post", $scope.user) .success(function (data, header, config, status) { console.log(data); }) .error(function (data, header, config, status) { console.log(data); }) ; }
4、由Controller处理请求并返回结果
get
@RequestMapping("/get") @ResponseBody public Map<String,String> get(String param) { System.out.println("param:"+param); response.put("state", "success");//将数据放在Map对象中 return response; }
post
@RequestMapping("/post2") @ResponseBody public void post2(@RequestBody User user, HttpServletResponse resp) { //返回不同的http状态 if(user.getName()!=null&&!user.getName().equals("")){ resp.setStatus(200); } else{ resp.setStatus(300); } }
如果需要配置请求头部
$http({ method : "POST", url : "/post", data : $scope.user }).success(function(data, header, config, status) { console.log(data); }).error(function(data, header, config, status) { console.log(data); });
5、由JS http请求的回调函数处理并执行下一步操作
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Request</title> </head> <body ng-app="MyApp" ng-controller="MyCtrl" > get:<input type="text" ng-model="param"><br> post: <input type="text" ng-model="user.name"><input type="text" ng-model="user.password"><br> <button ng-click="get()">Get</button> <button ng-click="post()">Post</button> </body> <script src="js/angular.min.js"></script> <script src="js/sbt.js"></script> </html>
sbt.js
var app = angular.module("MyApp", []); app.controller("MyCtrl", function ($scope, $http) { $scope.get = function () { $http.get("/get", {params: {param: $scope.param}}) .success(function (data, header, config, status) { console.log(data); }) .error(function (response) { console.log(response); }) ; } $scope.post = function () { $http.post("/post", $scope.user) .success(function (data, header, config, status) { console.log(data); }) .error(function (data, header, config, status) { console.log(data); }) ; } });
以上这篇AngularJS发送异步Get/Post请求方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
内容总结
以上是互联网集市为您收集整理的AngularJS发送异步Get/Post请求方法全部内容,希望文章能够帮你解决AngularJS发送异步Get/Post请求方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。