AngularJS $http模塊POST請求實現(xiàn)
更新時間:2017年04月08日 11:32:09 作者:IM楊燕飛
本篇文章主要介紹了AngularJS $http模塊POST請求實現(xiàn),這里整理了詳細的代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
一、代碼如下:
$http({
method:'post',
url:'post.php',
data:{name:"aaa",id:1,age:20}
}).success(function(req){
console.log(req);
})
解決方案:
1、
var myApp = angular.module('app',[]);
myApp.config(function($httpProvider){
$httpProvider.defaults.transformRequest = function(obj){
var str = [];
for(var p in obj){
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
2.
$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("&");
}
}).success(function(req){
console.log(req);
})
php
$rawpostdata = file_get_contents("php://input");
$post = json_decode($rawpostdata, true);
//傳的數(shù)據(jù)都在$post中了;
二、 $http請求數(shù)據(jù)主要會有以下三種方式
1.get請求
2.post請求
3.jsonp
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<title>Angular基礎(chǔ)</title>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="personCtrl">
姓:<input type="text" ng-model="firstName"/><br/>
名:<input type="text" ng-model="lastName"/><br/>
姓名:<span ng-bind="firstName"></span><span ng-bind="lastName"></span>
</div>
</div>
<script src="angular.min.js"></script>
<script type="application/javascript">
var myApp=angular.module('myApp',[]);
myApp.controller('personCtrl',function($scope,$http){
$http.get('getData.php').
success(function(data) {
console.log(data);
}).
error(function(err) {
//錯誤代碼
});
//$http.post采用postJSON方式發(fā)送數(shù)據(jù)到后臺,
// 解決辦法:在后臺php中使用$postData=file_get_contents("php://input",true);這樣就可以獲得前端傳送過來的數(shù)據(jù)
var postData={msg:'post的內(nèi)容'};
var config={params:{id:'5',name:'張三豐'}};
$http.post('postData.php', postData,config).
success(function(data) {
console.log(data);
}).
error(function(err) {
//錯誤代碼
});
var myUrl ="http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1&callback=JSON_CALLBACK";
$http.jsonp(myUrl).success(
function(data){
console.log(data);
}
).error(function(err){
//錯誤代碼
});
$scope.firstName="Wang";
$scope.lastName="Ben";
});
</script>
</body>
</html>
<?php
//postData.php文件
//用接收json數(shù)據(jù)的方式
$msg=file_get_contents("php://input",true);
$name=$_GET['name'];
echo $name.$msg."_post";
顯示效果:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 解決angular的$http.post()提交數(shù)據(jù)時后臺接收不到參數(shù)值問題的方法
- 對比分析AngularJS中的$http.post與jQuery.post的區(qū)別
- Angularjs中$http以post請求通過消息體傳遞參數(shù)的實現(xiàn)方法
- 后端接收不到AngularJs中$http.post發(fā)送的數(shù)據(jù)原因分析及解決辦法
- AngularJS下$http服務(wù)Post方法傳遞json參數(shù)的實例
- AngularJS $http post 傳遞參數(shù)數(shù)據(jù)的方法
- angularJS 發(fā)起$http.post和$http.get請求的實現(xiàn)方法
- AngularJS封裝$http.post()實例詳解
- 深入理解Angularjs中$http.post與$.post
- Angular利用HTTP POST下載流文件的步驟記錄
相關(guān)文章
詳解Angular-ui-BootStrap組件的解釋以及使用
這篇文章主要介紹了詳解Angular-ui-BootStrap組件的解釋以及使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
利用Angularjs中模塊ui-route管理狀態(tài)的方法
這篇文章主要給大家介紹了如何用Angularjs中的模板ui-route來管理狀態(tài)的方法,文中通過示例代碼介紹的很詳細,相信對大家的理解和學習具有一定的參考借鑒價值,有需要的朋友可以一起來學習學習。2016-12-12

