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

前端 未结 22 2265
庸人自扰
庸人自扰 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 00:42

    This is what I am doing for my need, Where I need to send the login data to API as form data and the Javascript Object(userData) is getting converted automatically to URL encoded data

            var deferred = $q.defer();
            $http({
                method: 'POST',
                url: apiserver + '/authenticate',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                transformRequest: function (obj) {
                    var str = [];
                    for (var p in obj)
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    return str.join("&");
                },
                data: userData
            }).success(function (response) {
                //logics
                deferred.resolve(response);
            }).error(function (err, status) {
               deferred.reject(err);
            });
    

    This how my Userdata is

    var userData = {
                    grant_type: 'password',
                    username: loginData.userName,
                    password: loginData.password
                }
    

提交回复
热议问题