AngularJs $http.post() does not send data

前端 未结 30 1810
我在风中等你
我在风中等你 2020-11-22 02:51

Could anyone tell me why the following statement does not send the post data to the designated url? The url is called but on the server when I print $_POST - I get an empty

30条回答
  •  情话喂你
    2020-11-22 03:19

    You can set the default "Content-Type" like this:

    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
    

    About the data format:

    The $http.post and $http.put methods accept any JavaScript object (or a string) value as their data parameter. If data is a JavaScript object it will be, by default, converted to a JSON string.

    Try to use this variation

    function sendData($scope) {
        $http({
            url: 'request-url',
            method: "POST",
            data: { 'message' : message }
        })
        .then(function(response) {
                // success
        }, 
        function(response) { // optional
                // failed
        });
    }
    

提交回复
热议问题