Send angular POST request

I want to send an angular message request. My angular problem really sends a receive request instead of a mail request. My angular request is here:

$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));
    });

When I debug a request with a browser network tab, I see the request parameter in the URL of my server. what should I do?

+4
source share
2 answers

I would write this as follows:

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));
     });
+2
source

Try the following:

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;
            });
    }
0
source

Source: https://habr.com/ru/post/1666402/


All Articles