0

For some reason I can't get the post variables from the controller

The AJAX/Javascript

function uploadImage(userActionPath,type)
{

    if( (userActionPath == 'undefined') || (type == 'undefined')) {
        console.error("no parameters for function uploadImage defined");
    }

    if((base64code == 'undefined') || (base64code == null))
    {
        console.error("please select an image");
    }

    var xml = ( window.XMLHttpRequest ) ?
            new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

    alert(base64code); //<- shows the base64 code, so its there

    var params = userActionPath+"?imagebase64="+base64code+"&type="+type;

    xml.open("POST",userActionPath,true);
    xml.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

    xml.onreadystatechange = function()
    {
        if( xml.readyState === 4 && xml.status === 200 )
        {
            var serverResponse = JSON.parse(xml.responseText);

            switch(serverResponse.f)
            {
                case 0:
                    console.log('love sosa'); //<- I get the response
                    break;
            }
        }
    };
    xml.send(params);
}

The controller

class LiveuploadController extends Controller
{
    /**
     * @Route("/LiveUpload",name="fileLiveUpload")
     * @Template()
     */
    public function indexAction(Request $request)
    {
        //I have tried these but 'imagebase64' returns null
        //returns null 
          $value = $request->request->get('imagebase64');
        //returns null
          $value = $request->query->get('imagebase64');
       //returns null 
          $value = $this->get('request')->request->get('imagebase64');

        $response = array('f'=>0,'base64'=>$value);
        return new Response(json_encode($response));
    }
}

The request headers also show that the variables are being sent.But both the type AND the imagebase64 variables return null on the controller

user3531149
  • 1,409
  • 1
  • 27
  • 46

2 Answers2

2

The problem is with the way that you have setup the XmlHttpRequest. You have set it up like it should be using GET, but when you want to POST, it is a bit different. Take a look at this question for more info on how to send a POST request. The quick and dirty of it is:

var xml = ( window.XMLHttpRequest ) ?
        new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
var params = "imagebase64="+base64code+"&type="+type;
xml.open("POST", userActionPath, true);

xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xml.setRequestHeader("Content-length", params.length);
xml.setRequestHeader("Connection", "close");

xml.onreadystatechange = function()
{
    if( xml.readyState === 4 && xml.status === 200 )
    {
        var serverResponse = JSON.parse(xml.responseText);

        switch(serverResponse.f)
        {
            case 0:
                console.log('love sosa'); //<- I get the response
                break;
        }
    }
};
xml.send(params);

In your example code, you are setting the header to expect JSON, but your params are urlencoded. Setting the proper header should do the trick.

And in your controller, if you are using POST, then you should get the request variables like this:

// Use this for getting variables of POST requests
$value = $request->request->get('imagebase64');

// This is used for getting variables of GET requests
$value = $request->query->get('imagebase64');
Community
  • 1
  • 1
Sehael
  • 3,468
  • 15
  • 31
1

This line of code in your JS:

xml.open("POST",userActionPath,true);

You are actually supplying userActionPath instead of params variable. It should be:

xml.open("POST",params,true);

As for the controller's code you should use:

$value = $request->query->get('imagebase64');

Hope this helps...

Jovan Perovic
  • 18,767
  • 5
  • 39
  • 76
  • so then where is the controller path supposed to be passed? Following this instruction the params should be sent with the send() method http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp – user3531149 May 12 '14 at 14:16
  • Ahh sorry about that, I missed that. In that case, check you `params` variable - it's value is concatenation of `userActionPath` and your parameters. Try removing `userActionPath` prefix... – Jovan Perovic May 12 '14 at 14:22
  • also tried $value = $request->get('imagebase64', 'nothing found'); and it returns 'nothing found' – user3531149 May 12 '14 at 14:29
  • OK, this proves to be more elusive that I thought :) I assume you have `Firebug` installed. Try firing it up and sending your request again... – Jovan Perovic May 12 '14 at 14:46