淺談angularjs $http提交數(shù)據(jù)探索
前兩天在搞自己的項(xiàng)目,前端js框架用的是angularjs框架;網(wǎng)站整的差不多的時(shí)候出事了;那就是當(dāng)我用$http.post()方法向服務(wù)器提交一些數(shù)據(jù)的時(shí)候;后臺(tái)總是接收不到數(shù)據(jù);
于是采用了其他方法暫時(shí)性替代一下;
今天正好有時(shí)間研究這個(gè)事情;網(wǎng)上查了很多資料;都試了試;都是不太好;但是還是給我提供了一些解決問(wèn)題的思路;
正文開(kāi)始:首先做了個(gè)demo如下;主要是為了比較他們的不同之處;

html如下:
<div class="container-fluid" data-ng-app="jjd" data-ng-controller="index">
<div class="container">
<div class="row">
<div class="col-md-5">
<p class="h4 text-center">jQ的$.post()提交</p>
<p> </p>
<div class="form-group">
<label for="exampleInputEmail1">用戶名</label>
<input type="text" ng-model="sdata.name" class="form-control" placeholder="用戶名">
</div>
<div class="form-group">
<label for="">密碼</label>
<input type="password" ng-model="sdata.pwd" class="form-control" placeholder="密碼">
</div>
<button type="button" class="btn btn-primary btn-block" ng-click="jPostData()">jQ提交</button>
</div>
<div class="col-md-2"> </div>
<div class="col-md-5">
<p class="h4 text-center">angularjs的$http.post()功能</p>
<p> </p>
<div class="form-group">
<label for="exampleInputEmail1">用戶名</label>
<input type="text" ng-model="sdata2.name" class="form-control" placeholder="用戶名">
</div>
<div class="form-group">
<label for="">密碼</label>
<input type="password" ng-model="sdata2.pwd" class="form-control" placeholder="密碼">
</div>
<button type="button" class="btn btn-primary btn-block" ng-click="aPostData()">$http提交</button>
</div>
</div>
</div>
</div>
js代碼如下:
var app = angular.module('jjd',[]);
app.controller('index',function($scope,$http){
$scope.sdata = {
name:'jQuery',
pwd:'jQuery'
};
$scope.sdata2 = {
name:'Angularjs',
pwd:'Angularjs'
};
/*jQ的ajax提交*/
$scope.jPostData = function(){
//console.log($scope.sdata);
$.post('/web/data.php',$scope.sdata,function(d){
console.log(d);
})
};
/*angularjs的$http提交*/
$scope.aPostData = function(){
$http({
url: '/web/data.php',
method: 'POST',
data:$scope.sdata2
}
}).success(function(da){
console.log(da);
});
};
});
后臺(tái)采用php的$_POST接收:
<?php
header("Content-type: text/html; charset=utf-8");
$aname = $_POST['name'];
$apwd = $_POST['pwd'];
$msg = array();
$msg['name'] = $aname;
$msg['pwd'] = $apwd;
echo json_encode($msg);
?>
服務(wù)器采用wampsever的本地啟動(dòng)的本地服務(wù)器。致此,頁(yè)面服務(wù)搭建完畢;開(kāi)始測(cè)試;
結(jié)果如圖:

從上圖的對(duì)比中可以看出后臺(tái)接收不到值得原因主要是1、2、3處不同;
其中1和2是請(qǐng)求的頭文件信息;
3是數(shù)據(jù)類型不同;jq發(fā)送的是Form Data;而angularjs默認(rèn)發(fā)送的是json數(shù)據(jù);
產(chǎn)生原因已經(jīng)找到;下面開(kāi)始改造;
首先針對(duì)1和2,在$http()的方法中配置header信息;
其次對(duì)數(shù)據(jù)進(jìn)行轉(zhuǎn)換;這里我做了個(gè)簡(jiǎn)單的json到string轉(zhuǎn)換的服務(wù);
改造后的代碼如下:
/*------創(chuàng)建angularjs應(yīng)用------*/
var app = angular.module('jjd',[]);
/*創(chuàng)建json格式到string的轉(zhuǎn)換服務(wù)*/
app.service('jsonToStr',function(){
this.transform = function(jsonData){
var string = '';
for(str in jsonData){
string = string + str +'=' + jsonData[str] +'&';
}
var _last = string.lastIndexOf('&');
string = string.substring(0,_last);
return string;
};
});
/*---------首頁(yè)控制器--------*/
app.controller('index',function($scope,$http,jsonToStr){//注入創(chuàng)建的jsonToStr服務(wù)
$scope.sdata = {
name:'jQuery',
pwd:'jQuery'
};
$scope.sdata2 = {
name:'Angularjs',
pwd:'Angularjs'
};
/*jQ的ajax提交*/
$scope.jPostData = function(){
//console.log($scope.sdata);
$.post('/web/data.php',$scope.sdata,function(d){
console.log(d);
})
};
/*angularjs的$http提交*/
$scope.aPostData = function(){
//console.log(jsonToStr.transform($scope.sdata2));
$http({
url: '/web/data.php',
method: 'POST',
data:$scope.sdata2,
data: jsonToStr.transform($scope.sdata2),//對(duì)提交的數(shù)據(jù)格式化
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).success(function(da){
console.log(da);
});
};
});
致此,angularjs提交數(shù)據(jù)后臺(tái)接收不到的問(wèn)題解決(只針對(duì)json數(shù)據(jù)格式);獻(xiàn)給奮斗中的小伙伴;
這個(gè)問(wèn)題應(yīng)該還有一種思路;就是在服務(wù)端進(jìn)行對(duì)獲取json格式的數(shù)據(jù)的支持,有興趣的小伙伴可以嘗試一下;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 簡(jiǎn)介AngularJS中$http服務(wù)的用法
- 對(duì)比分析AngularJS中的$http.post與jQuery.post的區(qū)別
- 詳解AngularJS中$http緩存以及處理多個(gè)$http請(qǐng)求的方法
- 詳解AngularJS中的http攔截
- AngularJS中$http服務(wù)常用的應(yīng)用及參數(shù)
- 后端接收不到AngularJs中$http.post發(fā)送的數(shù)據(jù)原因分析及解決辦法
- AngularJS通過(guò)$http和服務(wù)器通信詳解
- AngularJS出現(xiàn)$http異步后臺(tái)無(wú)法獲取請(qǐng)求參數(shù)問(wèn)題的解決方法
- angularJS之$http:與服務(wù)器交互示例
- AngularJS中$http使用的簡(jiǎn)單介紹
相關(guān)文章
Angular性能優(yōu)化之第三方組件和懶加載技術(shù)
這篇文章主要介紹了Angular性能優(yōu)化之第三方組件和懶加載技術(shù),對(duì)性能優(yōu)化感興趣的同學(xué),可以參考下2021-05-05
angular4 如何在全局設(shè)置路由跳轉(zhuǎn)動(dòng)畫的方法
本篇文章主要介紹了angular4 如何在全局設(shè)置路由跳轉(zhuǎn)動(dòng)畫的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
在Angular中實(shí)現(xiàn)懶加載的示例代碼
在Angular中,懶加載技術(shù)通過(guò)路由配置實(shí)現(xiàn)模塊的按需加載,可優(yōu)化應(yīng)用啟動(dòng)時(shí)間和減少初始加載代碼量,首先創(chuàng)建獨(dú)立模塊,在模板中使用<router-outlet>插入懶加載組件,并可采用預(yù)加載策略如PreloadAllModules,以提前加載所有懶加載模塊,優(yōu)化加載性能2024-10-10
AngularJS 過(guò)濾與排序詳解及實(shí)例代碼
這篇文章主要介紹了AngularJS 過(guò)濾與排序,這里整理了詳細(xì)的資料及簡(jiǎn)單實(shí)例代碼,有需要的小伙伴可以參考下2016-09-09
AngularJS實(shí)現(xiàn)與Java Web服務(wù)器交互操作示例【附demo源碼下載】
這篇文章主要介紹了AngularJS實(shí)現(xiàn)與Java Web服務(wù)器交互操作的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了AngularJS前臺(tái)ajax提交與javascript后臺(tái)處理的完整流程與實(shí)現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-11-11

