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

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

    AngularJS is doing it right as it doing the following content-type inside the http-request header:

    Content-Type: application/json
    

    If you are going with php like me, or even with Symfony2 you can simply extend your server compatibility for the json standard like described here: http://silex.sensiolabs.org/doc/cookbook/json_request_body.html

    The Symfony2 way (e.g. inside your DefaultController):

    $request = $this->getRequest();
    if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
        $data = json_decode($request->getContent(), true);
        $request->request->replace(is_array($data) ? $data : array());
    }
    var_dump($request->request->all());
    

    The advantage would be, that you dont need to use jQuery param and you could use AngularJS its native way of doing such requests.

提交回复
热议问题