angularJS之$http:与服务器交互示例
网络编程 2021-07-04 19:20www.168986.cn编程入门
$http是angular中的一个核心服务,本篇文章主要介绍了angularJS之$http:与服务器交互示例,具有一定的参考价值,有兴趣的可以了解一下。
在angularJS中与远程HTTP服务器交互时会用一个非常关键的服务-$http。
- $http是angular中的一个核心服务,利用浏览器的xmlhttprequest或者via JSONP对象与远程HTTP服务器进行交互。
- $http的使用方式和jquery提供的$.ajax操作比较相同,均支持多种method的请求,get、post、put、delete等。
- $http的各种方式的请求更趋近于rest风格。
- 在controller中可通过与$scope同样的方式获取$http对象,e.g. function controller($scope,$http){}
狼蚁网站SEO优化进行$http服务的使用说明,调用如下:
代码如下:
$http(config).suess(function(data,status,headers,config){}).error(function(data,status,headers,config){});
1.config为一个JSON对象,其中主要包含该请求的url、data、method等,如{url:"login.do",method:"post",data:{name:"12346",pwd:"123"}}。
- method {String} 请求方式e.g. "GET"."POST"
- url {String} 请求的URL地址
- params {key,value} 请求参数,将在URL上被拼接成?key=value
- data {key,value} 数据,将被放入请求内发送至服务器
- cache {boolean} 若为true,在http GET请求时采用默认的$http cache,否则使用$cacheFactory的实例
- timeout {number} 设置超时时间
2、suess为请求成功后的回调函数,error为请求失败后的回调函数,这里主要是对返回的四个参数进行说明。
- data 响应体
- status 相应的状态值
- headers 获取getter的函数
- config 请求中的config对象,同上第1点
为了方便大家与HTTP服务器进行交互,angularJS提供了各个请求方式下方法。
$http.put/post(url,data,config) url、name必填,config可选
$http.get/delete/jsonp/head(url,confid) url必填,config可选
url、data、config与$http的参数一致,
狼蚁网站SEO优化有一个simple demo用于展示如何使用$http()及$http.post()。
<!DOCTYPE HTML> <html lang="zh-" > <head> <meta charset="UTF-8"> <title>CSSClasses</title> <script src="angular.min.js" type="text/javascript"></script> <script type="text/javascript"> function ctrl($http,$scope){ $scope.login = function(user){ $http.post("login.do",user).suess(function(data, status, headers, config){ alert("suess"); }).error(function(data, status, headers, config){ alert("error"); }) } $scope.login1 = function(user){ $http({url:"login.do",data:user}).suess(function(data, status, headers, config){ alert("suess"); }).error(function(data, status, headers, config){ alert("error"); }) } } </script> </head> <body ng-app> <div ng-controller="ctrl"> <form name="loginFm"> Name:<input ng-model="user.name" /> pwd: <input ng-model="user.pwd" /> <input type="button" value="login" ng-click="login(user)" /> <input type="button" value="login1" ng-click="login1(user)" /> </form> </div> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程