AngularJS發起$http.post請求收不到返回的數據,結果為null


解決方案:

  1. 配置$httpProvider:
    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("&");  
       }  
    
       $httpProvider.defaults.headers.post = {  
            'Content-Type': 'application/x-www-form-urlencoded'  
       }  
    
    });  

     

  2. 或者在post中配置:
    $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);  
    })  

     


免責聲明!

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



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