繁体   English   中英

发送角度POST请求

[英]Send an angular POST Request

我想通过angular发送一个帖子请求。 我的问题是角度实际上发送一个get请求而不是post请求。 我的角度要求在这里:

$http({
        method: 'POST',
        url: pages_url,
        params: {
            'page': $scope.current_page_id,
            'news': JSON.stringify(news),
            'method': 'POST'
        }
    }).then(function (response) {
        alert(JSON.stringify(response));
    });

当我使用浏览器的网络选项卡调试请求时,我在服务器URL中看到请求的参数。 我该怎么办?

我会这样写:

var req_body = {
    page: $scope.current_page_id,
    news: JSON.stringify(news),
    method: 'POST' // <- is this really a parameter you want or do you misunderstood the post as a request?
};
$http.post(pages_url, req_body)
     .then(function (response) {
         alert(JSON.stringify(response));
     });

尝试这个:

function yourFunction(param1, param2) { 
        return $http({ 
            method: 'POST', 
            url: yourUrl, 
            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("&"); 
            }, 
            data: {username: param1, password: param2} 
            })
           .then(function(response) {
                return response;
            });
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM