使用angularjs的$http.post異步提交數據時,服務器接收不了的問題


一,在正常情況下,使用表單的post方法提交數據,默認請求頭的Content-Type:application/x-www-form-urlencoded類型,

提交數據格式如下:

二,使用angularjs的$http.post提交數據,使用的是Content-Type:application/json類型,

請求頭格式如下:

直接代碼塊:

 1         app.controller('payCtrl',function($scope,$http){
 2             //保存郵箱地址
 3             $scope.emailEditSave=function(e){
 4                 e=e || window.event;
 5                 preventSubmit(e);
 6                 var yes=confirm('是否確認更改或者添加郵箱地址?');
 7                 if(yes ==true){
 8                     $http.post('http://localhost/html/angular_post.php',{email:"liang@163.com",cEmail:"liang@163.com"})
 9                             .success(function(resp){
10                                 console.log(resp);
11                             });
12                 }
13 
14             };
15         })

三,所以把angularjs默認的json類型定義為正常application/x-www-form-urlencoded類型,同時把提交的數據序列化

請求頭如下:

直接代碼塊:

 1   var app=angular.module('payApp',[],function($httpProvider) {
 2             // Use x-www-form-urlencoded Content-Type
 3             $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
 4 
 5             /**
 6              * The workhorse; converts an object to x-www-form-urlencoded serialization.
 7              * @param {Object} obj
 8              * @return {String}
 9              */
10             var param = function(obj) {
11                 var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
12 
13                 for(name in obj) {
14                     value = obj[name];
15 
16                     if(value instanceof Array) {
17                         for(i=0; i<value.length; ++i) {
18                             subValue = value[i];
19                             fullSubName = name + '[' + i + ']';
20                             innerObj = {};
21                             innerObj[fullSubName] = subValue;
22                             query += param(innerObj) + '&';
23                         }
24                     }
25                     else if(value instanceof Object) {
26                         for(subName in value) {
27                             subValue = value[subName];
28                             fullSubName = name + '[' + subName + ']';
29                             innerObj = {};
30                             innerObj[fullSubName] = subValue;
31                             query += param(innerObj) + '&';
32                         }
33                     }
34                     else if(value !== undefined && value !== null)
35                         query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
36                 }
37 
38                 return query.length ? query.substr(0, query.length - 1) : query;
39             };
40 
41             // Override $http service's default transformRequest
42             $httpProvider.defaults.transformRequest = [function(data) {
43                 return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
44             }];
45 
46         });
47 
48         app.controller('payCtrl',function($scope,$http){
49             //保存郵箱地址
50             $scope.emailEditSave=function(e){
51                 e=e || window.event;
52                 preventSubmit(e);
53                 var yes=confirm('是否確認更改或者添加郵箱地址?');
54                 if(yes ==true){
55                     $http.post('http://localhost/html/angular_post.php',{email:"liang@163.com",cEmail:"liang@163.com"})
56                             .success(function(resp){
57                                 console.log(resp);
58                             });
59                 }
60 
61             };
62         })
63 
64         //阻止默認提交
65         function preventSubmit(e){
66             if(document.all){
67                 e.returnValue;
68             }else{
69                 e.preventDefault();
70             }
71         }

主要是在angular.module()添加一個出來更改Content-type和序列化正常表單提交數據格式的函數,接着$http.post提交后的數據服務器就可正常獲取。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM