How can I post data as form data instead of a request payload?

前端 未结 22 2632
庸人自扰
庸人自扰 2020-11-22 00:13

In the code below, the AngularJS $http method calls the URL, and submits the xsrf object as a \"Request Payload\" (as described in the Chrome debugger network t

22条回答
  •  感动是毒
    2020-11-22 00:52

    I'm currently using the following solution I found in the AngularJS google group.

    $http
    .post('/echo/json/', 'json=' + encodeURIComponent(angular.toJson(data)), {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
    }).success(function(data) {
        $scope.data = data;
    });
    

    Note that if you're using PHP, you'll need to use something like Symfony 2 HTTP component's Request::createFromGlobals() to read this, as $_POST won't automatically loaded with it.

提交回复
热议问题