How to use Js->submit() in CakePHP?

前端 未结 2 407
被撕碎了的回忆
被撕碎了的回忆 2021-01-14 03:11

Im trying to create a simple Ajax form for a message board in a CakePHP application, but I can\'t for the life of me figure out how to properly use the Js->submit() function

相关标签:
2条回答
  • 2021-01-14 03:28

    What happens is that it loads the default layout. You'll have to change the layout to ajax with the following line:

    $this->layout = 'ajax';
    

    You insert that line inside your isAjax() condition.

    Also your options array has the wrong format. The action key should be inside the url key.

    $this->Js->submit('Post Your Message', array(
            'url' => array(
                'action' => 'add'
            ),
            'update' => '#message_board'
        )
    );
    
    0 讨论(0)
  • 2021-01-14 03:33

    I am very late to the party! But a while back, about 3months ago I faced this problem. And what it turned out to be was that I was not giving permission to the user to access that action. The solution was to add some code in the AppController beforeFilter(), something like :

    function beforeFilter(){
        $this->Auth->allow('my_actions_name');
    }
    

    However I am not really sure why it re-renders the parent div, or sometimes the whole page, but my guess is, that cake is set so that whenever the action is not accessible/allowed, some routing takes place and perhaps index or other default actions, are executed, which causes that to be rendered within your ajax view.

    If you inspect the request response inside the browser's network tab( in chrome), it becomes very clear that the response returned is the same content that is being rendered. However, I am a complete newbie and the is answer is based on speculation.

    If anyone can give a better explanation(please teach us), I will be the first to upvote :)

    Till then, Happy coding

    0 讨论(0)
提交回复
热议问题