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

3
  • 1
    $value = $request->request->get('imagebase64'); - this should work. You're overwriting $value on the next lines. Commented May 12, 2014 at 13:14
  • I know the value is being overwritten, I'm just showing what I've tried so far, maybe I should write it in a less confusing way Commented May 12, 2014 at 13:19
  • that one is returning null also, maybe it has something to do with the ajax? Commented May 12, 2014 at 13:24

2 Answers 2

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Refused to set unsafe header "Content-length" front:198 Refused to set unsafe header "Connection"
but the first one worked, god bless your soul I've been working on this for hours.
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...

4 Comments

so then where is the controller path supposed to be passed? Following this instruction the params should be sent with the send() method w3schools.com/ajax/ajax_xmlhttprequest_send.asp
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...
also tried $value = $request->get('imagebase64', 'nothing found'); and it returns 'nothing found'
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.