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

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

    In your app config -

    $httpProvider.defaults.transformRequest = function (data) {
            if (data === undefined)
                return data;
            var clonedData = $.extend(true, {}, data);
            for (var property in clonedData)
                if (property.substr(0, 1) == '$')
                    delete clonedData[property];
    
            return $.param(clonedData);
        };
    

    With your resource request -

     headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
    
    0 讨论(0)
  • 2020-11-22 00:53

    The only thin you have to change is to use property "params" rather than "data" when you create your $http object:

    $http({
       method: 'POST',
       url: serviceUrl + '/ClientUpdate',
       params: { LangUserId: userId, clientJSON: clients[i] },
    })
    

    In the example above clients[i] is just JSON object (not serialized in any way). If you use "params" rather than "data" angular will serialize the object for you using $httpParamSerializer: https://docs.angularjs.org/api/ng/service/$httpParamSerializer

    0 讨论(0)
  • 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:

    <?php
    
    namespace Acme\Test\MyRequest;
    
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\ParameterBag;
    
    class MyRequest extends Request{
    
    
    /**
    * Override and extend the createFromGlobals function.
    * 
    * 
    *
    * @return Request A new request
    *
    * @api
    */
    public static function createFromGlobals()
    {
      // Get what we would get from the parent
      $request = parent::createFromGlobals();
    
      // Add the handling for 'application/json' content type.
      if(0 === strpos($request->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);
    
    0 讨论(0)
  • 2020-11-22 00:56

    These answers look like insane overkill, sometimes, simple is just better:

    $http.post(loginUrl, "userName=" + encodeURIComponent(email) +
                         "&password=" + encodeURIComponent(password) +
                         "&grant_type=password"
    ).success(function (data) {
    //...
    
    0 讨论(0)
提交回复
热议问题