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

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

    For Symfony2 users:

    If you don't want to change anything in your javascript for this to work you can do these modifications in you symfony app:

    Create a class that extends Symfony\Component\HttpFoundation\Request class:

    headers->get('CONTENT_TYPE'), 'application/json')){
    
        // The json is in the content
        $cont = $request->getContent();
    
        $json = json_decode($cont);
    
        // ParameterBag must be an Array.
        if(is_object($json)) {
          $json = (array) $json;
      }
      $request->request = new ParameterBag($json);
    
    }
    
    return $request;
    
    }
    
    }
    

    Now use you class in app_dev.php (or any index file that you use)

    // web/app_dev.php
    
    $kernel = new AppKernel('dev', true);
    // $kernel->loadClassCache();
    $request = ForumBundleRequest::createFromGlobals();
    
    // use your class instead
    // $request = Request::createFromGlobals();
    $response = $kernel->handle($request);
    $response->send();
    $kernel->terminate($request, $response);
    

提交回复
热议问题