Posting JSON data to FOSUserBundle Registration, Login, Profile, etc

I haven't seen any questions about sending requests by JSON to use FOSUserBundle's functions so I am posting this question. I have a Symfony2 project that is primarily designed for mobile use (AJAX too). I am using FOSRestBundle and FOSUserBundle which work well from the forms interface. However, I'd like to be able to register a user for example by posting JSON data to register, login, view profile, etc. How can this be done? Is it even possible to send JSON data to FOSUserBundle? If possible, I'd be glad if you could give an example of how to register or get the profile, etc.

Sample FOSUserBundle login form:

<form action="/projects/symfony/web/app_dev.php/login_check" method="post">
    <input type="hidden" name="_csrf_token" value="1cdb2c8fa2fb9a52905ddebff3185ac1a3ad708d" />

<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="" required="required" />

<label for="password">Password:</label>
<input type="password" id="password" name="_password" required="required" />

<input type="checkbox" id="remember_me" name="_remember_me" value="on" />
<label for="remember_me">Remember me</label>

<input type="submit" id="_submit" name="_submit" value="Login" />
 </form>
        </div>
    </body>
</html>

Answers


Here is a JSON example - it detects if user is logged in or not the "code" is the response returned by the action, the "imageHandler()" is the function that gets triggered using js in this case,for example using intervals or pressing a key.Of course it doesn't need parameters. the send function is used to send variables to the controller, in this case im using it to send the "imageId" variable with the value given in the parameter(myImageId), same counts for myToDo.

Javascript (This is the twig template)

    <script language="JavaScript" type="text/javascript">
      function imageHandler(myToDo,myimageId)
       { 
         var xml = ( window.XMLHttpRequest ) ?
             new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

         xml.open("GET", "{{path('imageHandler')}}", true); 
         xml.setRequestHeader("Content-type", "application/json"); 

        xml.onreadystatechange = function() 
        {
            var serverResponse = JSON.parse(xml.responseText);

             if(serverResponse.code == 100 && serverResponse.success)
               {
                       //success
                }
             else if(serverResponse.code == 200) 
               {
                   //not logged in

                }

         } 
            xml.send({imageId: myimageId, toDo: myToDo});   
     }
    </script>


   /* This is the controller, the javascript knows which controller to call by using the "name    annotation"
    */

      /**
        * @Route(name="imageHandler")
        */
        public function imageHandlerAjaxAction(Request $request)
        {
            //If user is not logged in..
            if (false === $this->get('security.context')->isGranted('ROLE_USER')) 
            {
                //return this variables (your choice of course, format: variable=>value)
                $response = array("code"=>200,"success"=>false);

                return new Response(json_encode($response));
            }
       }

Need Your Help

jQuery DataTables: control table width

jquery datatables jquery-datatables

I have a problem controlling the width of a table using the jQuery DataTables plugin. The table is supposed to be 100% of the container width, but ends up being an arbitrary width, rather less tha...

Formatting time for ical export

date datetime time icalendar rfc5545

I have created a calendar in jquery that exports to ical. However, I am having some issues with the datetime.