淺談angularjs $http提交數(shù)據(jù)探索
前兩天在搞自己的項目,前端js框架用的是angularjs框架;網(wǎng)站整的差不多的時候出事了;那就是當我用$http.post()方法向服務(wù)器提交一些數(shù)據(jù)的時候;后臺總是接收不到數(shù)據(jù);
于是采用了其他方法暫時性替代一下;
今天正好有時間研究這個事情;網(wǎng)上查了很多資料;都試了試;都是不太好;但是還是給我提供了一些解決問題的思路;
正文開始:首先做了個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); }); }; });
后臺采用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的本地啟動的本地服務(wù)器。致此,頁面服務(wù)搭建完畢;開始測試;
結(jié)果如圖:
從上圖的對比中可以看出后臺接收不到值得原因主要是1、2、3處不同;
其中1和2是請求的頭文件信息;
3是數(shù)據(jù)類型不同;jq發(fā)送的是Form Data;而angularjs默認發(fā)送的是json數(shù)據(jù);
產(chǎn)生原因已經(jīng)找到;下面開始改造;
首先針對1和2,在$http()的方法中配置header信息;
其次對數(shù)據(jù)進行轉(zhuǎ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; }; }); /*---------首頁控制器--------*/ 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),//對提交的數(shù)據(jù)格式化 headers: { 'Accept': '*/*', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } }).success(function(da){ console.log(da); }); }; });
致此,angularjs提交數(shù)據(jù)后臺接收不到的問題解決(只針對json數(shù)據(jù)格式);獻給奮斗中的小伙伴;
這個問題應(yīng)該還有一種思路;就是在服務(wù)端進行對獲取json格式的數(shù)據(jù)的支持,有興趣的小伙伴可以嘗試一下;
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 簡介AngularJS中$http服務(wù)的用法
- 對比分析AngularJS中的$http.post與jQuery.post的區(qū)別
- 詳解AngularJS中$http緩存以及處理多個$http請求的方法
- 詳解AngularJS中的http攔截
- AngularJS中$http服務(wù)常用的應(yīng)用及參數(shù)
- 后端接收不到AngularJs中$http.post發(fā)送的數(shù)據(jù)原因分析及解決辦法
- AngularJS通過$http和服務(wù)器通信詳解
- AngularJS出現(xiàn)$http異步后臺無法獲取請求參數(shù)問題的解決方法
- angularJS之$http:與服務(wù)器交互示例
- AngularJS中$http使用的簡單介紹
相關(guān)文章
深入理解Angularjs中的$resource服務(wù)
大家可以知道在Angularjs中可以用$http同服務(wù)器進行通信,功能上比較簡單,AngularJS還提供了另外一個可選的服務(wù)$resource,使用它可以非常方便的同支持restful的服務(wù)單進行數(shù)據(jù)交互。這篇文章主要給大家深入的介紹了Angularjs中的$resource服務(wù)。2016-12-12詳解在Angular4中使用ng2-baidu-map的方法
這篇文章主要介紹了在Angular4中使用ng2-baidu-map的方法,本文分步驟給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06angular中實現(xiàn)li或者某個元素點擊變色的兩種方法
本篇文章主要介紹了angular中實現(xiàn)li或者某個元素點擊變色的兩種方法,非常具有實用價值,需要的朋友可以參考下2017-07-07詳解如何為你的angular app構(gòu)建一個第三方庫
這篇文章主要介紹了詳解如何為你的angular app構(gòu)建一個第三方庫,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12Angular實現(xiàn)可刪除并計算總金額的購物車功能示例
這篇文章主要介紹了Angular實現(xiàn)可刪除并計算總金額的購物車功能,涉及AngularJS事件響應(yīng)、元素遍歷與數(shù)值運算等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12