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

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

    The following line needs to be added to the $http object that is passed:

    headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    

    And the data passed should be converted to a URL-encoded string:

    > $.param({fkey: "key"})
    'fkey=key'
    

    So you have something like:

    $http({
        method: 'POST',
        url: url,
        data: $.param({fkey: "key"}),
        headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    })
    

    From: https://groups.google.com/forum/#!msg/angular/5nAedJ1LyO0/4Vj_72EZcDsJ

    UPDATE

    To use new services added with AngularJS V1.4, see

    • URL-encoding variables using only AngularJS services

提交回复
热议问题