AngularJs $http.post() does not send data

前端 未结 30 1812
我在风中等你
我在风中等你 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:22

    Didn't find a complete code snippet of how to use $http.post method to send data to the server and why it was not working in this case.

    Explanations of below code snippet...

    1. I am using jQuery $.param function to serialize the JSON data to www post data
    2. Setting the Content-Type in the config variable that will be passed along with the request of angularJS $http.post that instruct the server that we are sending data in www post format.

    3. Notice the $htttp.post method, where I am sending 1st parameter as url, 2nd parameter as data (serialized) and 3rd parameter as config.

    Remaining code is self understood.

    $scope.SendData = function () {
               // use $.param jQuery function to serialize data from JSON 
                var data = $.param({
                    fName: $scope.firstName,
                    lName: $scope.lastName
                });
    
                var config = {
                    headers : {
                        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
                    }
                }
    
                $http.post('/ServerRequest/PostDataResponse', data, config)
                .success(function (data, status, headers, config) {
                    $scope.PostDataResponse = data;
                })
                .error(function (data, status, header, config) {
                    $scope.ResponseDetails = "Data: " + data +
                        "
    status: " + status + "
    headers: " + header + "
    config: " + config; }); };

    Look at the code example of $http.post method here.

提交回复
热议问题