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

前端 未结 22 2651
庸人自扰
庸人自扰 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:35

    Create an adapter service for post:

    services.service('Http', function ($http) {
    
        var self = this
    
        this.post = function (url, data) {
            return $http({
                method: 'POST',
                url: url,
                data: $.param(data),
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            })
        }
    
    }) 
    

    Use it in your controllers or whatever:

    ctrls.controller('PersonCtrl', function (Http /* our service */) {
        var self = this
        self.user = {name: "Ozgur", eMail: null}
    
        self.register = function () {
            Http.post('/user/register', self.user).then(function (r) {
                //response
                console.log(r)
            })
        }
    
    })
    

提交回复
热议问题