AngularJS:使用$http请求后台(post和get方式)

1.声明

当前内容主要用于学习和使用AngularJS向后台传递数据,主要有两种post和get请求

2. demo

1.初始化app并传递$http,此时就可以使用$http

angular.module('myApp', []).controller('userCtrl', function($scope, $http) {
    
    });

2.使用get方式请求数据:

$http({
    
    
			method: 'GET',
			url: 'http://localhost:8080/selectUserAllUsingPage'
		}).then(function successCallback(response) {
    
    
			// 获取返回结果数据
			var resultData=response.data;
			$scope.users=resultData;// 为对象绑定数据
		}, function errorCallback(response) {
    
    
			// 请求失败执行代码
			alert("请求访问后台数据失败!");
			
		});

其中参数必须使用?参数名=参数值方式....方式向后台传递参数

3.使用post方式请求并传递参数:

$http({
    
    
			method: 'POST',
			url: 'http://localhost:8080/insertUserSelective',
			data:{
    
    
				"userName":userName,
				"loginName":loginName,
				"loginPwd":loginPwd,
				"userType":userType,
				"userGroupId":userGroupId,
				"home":home,
				"email":email
			}
		}).then(function successCallback(response) {
    
    
			// 获取返回结果数据
			var resultData=response.data;
			$scope.users=resultData;// 为对象绑定数据
			initTableData(); // 再次请求数据
			$("#msgSuccess").show();
		}, function errorCallback(response) {
    
    
			// 请求失败执行代码
			alert("请求访问后台数据失败!");
			
		});

此时post方式传递数据为:data:{}//对象数据

请求成功!

猜你喜欢

转载自blog.csdn.net/weixin_45492007/article/details/113882734