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

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

    There is a really nice tutorial that goes over this and other related stuff - Submitting AJAX Forms: The AngularJS Way.

    Basically, you need to set the header of the POST request to indicate that you are sending form data as a URL encoded string, and set the data to be sent the same format

    $http({
      method  : 'POST',
      url     : 'url',
      data    : $.param(xsrf),  // pass in data as strings
      headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
    });
    

    Note that jQuery's param() helper function is used here for serialising the data into a string, but you can do this manually as well if not using jQuery.

提交回复
热议问题