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

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

    I took a few of the other answers and made something a bit cleaner, put this .config() call on the end of your angular.module in your app.js:

    .config(['$httpProvider', function ($httpProvider) {
      // Intercept POST requests, convert to standard form encoding
      $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
      $httpProvider.defaults.transformRequest.unshift(function (data, headersGetter) {
        var key, result = [];
    
        if (typeof data === "string")
          return data;
    
        for (key in data) {
          if (data.hasOwnProperty(key))
            result.push(encodeURIComponent(key) + "=" + encodeURIComponent(data[key]));
        }
        return result.join("&");
      });
    }]);
    

提交回复
热议问题