AngularJs $http.post() does not send data

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

    I use jQuery param with AngularJS post requrest. Here is a example ... create AngularJS application module, where myapp is defined with ng-app in your HTML code.

    var app = angular.module('myapp', []);
    

    Now let us create a Login controller and POST email and password.

    app.controller('LoginController', ['$scope', '$http', function ($scope, $http) {
        // default post header
        $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
        // send login data
        $http({
            method: 'POST',
            url: 'https://example.com/user/login',
            data: $.param({
                email: $scope.email,
                password: $scope.password
            }),
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function (data, status, headers, config) {
            // handle success things
        }).error(function (data, status, headers, config) {
            // handle error things
        });
    }]);
    

    I don't like to exaplain the code, it is simple enough to understand :) Note that param is from jQuery, so you must install both jQuery and AngularJS to make it working. Here is a screenshot.

    enter image description here

    Hope this is helpful. Thanks!

提交回复
热议问题