1. 程式人生 > >angular -- post請求該如何使用?

angular -- post請求該如何使用?

ucc ole code 後臺 blog turn cati 方法 obj

angularjs 的post 請求該如何調用?

簡單示例:

// post 攜帶參數訪問
$http({  
    method:‘post‘,  
    url:postUrl,  
    data:{name:"aaa",id:1,age:20}  
}).success(function(req){  
    console.log(req);  
});

上面這種方法還是有些問題,攜帶的參數並不能發送給後臺。結果為null,這是因為要轉換為 form data:

可以參考:

https://blog.csdn.net/fengzijinliang/article/details/51897991

解決示例:

$http({  
    method:‘post‘,  
    url:postUrl,  
    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); });

angular -- post請求該如何使用?