AngularJS中$http的交互问题
- 作者: 广州的段友互粉吧
- 来源: 51数据库
- 2021-09-03
这篇文章,主要是通过我们熟悉的jquery中ajax和jsonp的类型方式,总结一下$http的使用。
$http 是 angularjs 中的一个核心服务,用于读取远程服务器的数据。
1. angular中的ajax
写法一:
$http({
method: 'get', //可以改成post
url: '/someurl'
}).then(function successcallback(response) {
// 请求成功执行代码
}, function errorcallback(response) {
// 请求失败执行代码
});
示例:
var app = angular.module('myapp', []);
app.controller('sitectrl', function($scope, $http) {
$http({
method: 'get',
url: 'http://www.51sjk.com/Upload/Articles/1/0/280/280395_20210709000152002.php',
}).then(function successcallback(response) {
console.log(response.data);
}, function errorcallback(response) {
console.log('失败');
});
});
写法二:
①get请求
$http.get('/someurl',config).then(successcallback, errorcallback);
$http.get('/someurl',{params:{}}).then(successcallback, errorcallback);
示例:
$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('失败');
});
②post请求
$http.post('/someurl', data, config).then(successcallback, errorcallback);
示例:
$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('失败');
});
/*
原理解读:
首先,配置headers是因为,post提交时,使用的content-type是application/x-www-form-urlencoded,
而使用原生ajax的post请求如果不指定请求头requestheader,默认使用的content-type是text/plain;charset=utf-8,
在html中form的content-type默认值是content-type:application/x-www-form-urlencoded,所以要进行相应的配置。
然后,配置transformrequest是因为,如果参数是对象,需要转化一下。
*/
2.angular中的jsonp
$http({method:'jsonp',url:''}).success().error();
$http.jsonp('/someurl').success().error();
//这里要注意,跨域请求的url后一定要追加参数callback,并且callback的值是固定的,即json_callback,尽量不要去做任何改动
示例:
$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);
});
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,尽量不要去做任何改动。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
热点文章
Angular中响应式表单的三种更新值方法详析
7
Angularjs实现下拉框联动的示例代码
7
详解AngularJS跨页面传值(ui-router)
2
详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用
3
angular2路由切换改变页面title的示例代码
4
Angular2 组件间通过@Input @Output通讯示例
5
Angularjs中ng-repeat的简单实例
3
AngularJS 中ui-view传参的实例详解
4
浅谈Angular路由守卫
4
详解基于Angular4+ server render(服务端渲染)开发教程
4
