문제

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

도움이 되었습니까?

해결책

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');

다른 팁

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...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top