Yii2: Post Ajax to Controller Action - Bad Request - missing required parameters

Created on 9 Sep 2015  路  9Comments  路  Source: yiisoft/yii2

Hello there!

After consulting the irc channel, looking for documentation in the guide on that topic and looking for other people having the same problem without any result, I think this could be a bug or just non-documented behavior.

I'm using PHP 5.5

Bad Request (#400): Missing required parameters: form

I have prettyUrls enabled, which is why the post request is adressed to 'spot/edit-video'.
It works when there are no paremeters in the action.
Javascript part (I'm using www.riotjs.com as my clientside framework/UI lib):

        saveClip(e) {
            this.parent.toggle()
            var request = jQuery.post(
                url+'spot/edit-video',
                {
                    form :
                    {
                        title: this.title.value,
                        description: this.description.value,
                        path: this.video.path
                    }
                }
            )
            request.success(
                function()
                {
                    this.trigger('videoUpdated')
                }.bind(this)
            )
        }

PHP - in the Controller:

    public function actionEditVideo($form)
    {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $form = (object) $form;
        //do sql stuff
        return 'test';
    }

I think that the parameter should be able to be utilized for ajax post request calls.

If you know any documentation regarding to this issue, I'd be happy to read and update that documentation.

Most helpful comment

Just realised, By default only $_GET values are bound to an actions params.
You'll need to use Yii::$app->request->post('form'); within the action.

All 9 comments

Do you have any dev tools in your browser that will allow you to see what gets posted?
Not sure how jQuery handles javascript objects as params.

Chrome Dev Tools:

Console:

POST http://localhost/yiisimple/web/spot/edit-video 400 (Bad Request)

Form Data

form[title]:Cats
form[description]:Kitties playing around
form[path]:customer/5/videos/
_csrf:QTZKVnlSYVQ5bD0gLT4ZAAhBHCYgGDVnM0BnAQoaMCAFARklEDUpYQ==

As a comparison:
The Ajax Playground (www.yiiplayground.com/yii2/web/index.php?r=ajax/index)
uses jquery post too.. even with post data, but it doesn't get asked for in the controller action.

I hope this info is sufficient.
I'd like to know: Is post data supposed to be retrievable within the controller action by using the paremter? in my case - form or video (depending on the param name)

error

Just realised, By default only $_GET values are bound to an actions params.
You'll need to use Yii::$app->request->post('form'); within the action.

Is there a setting of changing this? I'd love to use the full power of optional params and typehinting.
e.g.

actionSaveVideo($doStuff = false)

or

actionDoStuff(boolean $confirm)

Still: I'd love to see if this is somewhere in the docs described. I'd be happy to extend the existing docs, as there are only very little use cases documented for ajax in yii2

When using get, I get this error:

Bad Request (#400): Invalid data received for parameter "video".

Docs about action params here.

Alright, that worked. Although I'd love to use post to avoid any get url limitations if existing (http://stackoverflow.com/questions/3091485/what-is-the-limit-on-querystring-get-url-parameters)

From what I understand, yii2 is not designed to accept post via action param (by default).
If someone knows any docs regarding this setting, I'd love to close this issue and improve the docs.

Thanks a lot for your time.

Won't be changed. This is by design.

Was this page helpful?
0 / 5 - 0 ratings