AngularJS中的交互 $http

来源:互联网 发布:胡门网络胡建伟 编辑:程序博客网 时间:2024/06/06 17:27

这篇文章,主要是通过我们熟悉的jquery中ajax和jsonp的类型方式,总结一下$http的使用。

$http 是 AngularJS中的一个核心服务,用于读取远程服务器的数据。

1. angular中的ajax

写法一:

$http({    method: 'GET',  //可以改成POST    url: '/someUrl'}).then(function successCallback(response) {        // 请求成功执行代码    }, function errorCallback(response) {        // 请求失败执行代码});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

示例:

var app = angular.module('myApp', []);    app.controller('siteCtrl', function($scope, $http) {    $http({        method: 'GET',        url: 'https://www.runoob.com/try/angularjs/data/sites.php',    }).then(function successCallback(response) {            console.log(response.data);        }, function errorCallback(response) {            console.log('失败');    });});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

写法二:

①GET请求

$http.get('/someUrl',config).then(successCallback, errorCallback); $http.get('/someUrl',{params:{}}).then(successCallback, errorCallback);
  • 1
  • 2

示例:

$http.get({    'http://10.30.24.12/emp-management/empDetail',    {params:{"id":3}}}).then(function successCallback(response) {        console.log(response.data.name);    }, function errorCallback(response) {        console.log('失败');});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

②POST请求

$http.post('/someUrl', data, config).then(successCallback, errorCallback);
  • 1

示例:

$http({      method:'post',      url:'post.php',      data:{name:"aaa",id:"1",age:"20"}  }).then(function successCallback(response) {        console.log(response);    }, function errorCallback(response) {        console.log('失败');});//但是,这时候你可能收不到返回的数据,结果为null,这是因为要转换成form data。 //解决方案(在post中进行相应配置):$http({     method:'post',     url:'post.php',     data:{name:"aaa",id:"1",age:"20"},     headers:{'Content-Type': 'application/x-www-form-urlencoded'},     transformRequest: function(obj) {       var str = [];       for(var p in obj){         str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));       }       return str.join("&");     }  }).then(function successCallback(response) {        console.log(response);    }, function errorCallback(response) {        console.log('失败');});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

2.angular中的jsonp

$http({method:'JSONP',url:''}).success().error();$http.jsonp('/someUrl').success().error();//这里要注意,跨域请求的url后一定要追加参数callback,并且callback的值是固定的,即JSON_CALLBACK,尽量不要去做任何改动
  • 1
  • 2
  • 3
  • 4

示例:

$http({     method: 'JSONP',     url: 'http://www.b.com/test.php?callback=JSON_CALLBACK' }).success(function(response){     console.log(response); }); $http.jsonp(    'http://www.b.com/test.php?callback=JSON_CALLBACK').success(function (response){     console.log(response); }); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3.最后,总结一下注意事项:

(1)代码里使用的.then()也可以写成.success().error(),但是v1.5中 $http 的 success 和 error 方法已废弃,使用 then 方法替代;(2)关于参数:用GET的时候就是params,用POST/PUT/PATCH/DELETE就是data;(3)$http.jsonp跨域请求的url后一定要追加参数callback,并且callback的值是固定的,即JSON_CALLBACK,尽量不要去做任何改动。
原创粉丝点击