简体   繁体   English

cakePHP使用ajax提交表单

[英]cakePHP submitting a form with ajax

I am submitting a form with ajax and I am trying to return a json response. 我正在使用ajax提交表单,并且试图返回json响应。 The problem is I get back missing view. 问题是我找回了视野。 When I add autoResponder=false I get nothing at all. 当我添加autoResponder = false时,我什么也没得到。 I am using cakephp 2.5 我正在使用cakephp 2.5

I have another action in the same controller which I can get a json response back. 我在同一控制器中还有另一个动作,可以返回json响应。 I use a standard ajax call. 我使用标准的ajax调用。 The difference with this action is I am using form data with multipart. 此操作的区别在于我正在使用多部分表单数据。 In the other ajax call I was able to add the extension .json to the url to tell cakephp to use that route. 在另一个ajax调用中,我能够将扩展名.json添加到URL中,以告诉cakephp使用该路由。

When I add .json to the forms action url I get a response back missing action. 当我将.json添加到表单操作网址中时,我得到了一个缺少操作的响应。

<form action="/btstadmin/pages/ajax_edit.json/3" id="ajax_editJsonForm" enctype="multipart/form-data" method="post" accept-charset="utf-8">

The response back 回复回来

The action <em>ajax_edit.json</em> is not defined in controller <em>PagesController

My action is 我的行动是

public function ajax_edit() {

    if ($this->request->is('ajax')) {
        if ($this->Page->save($this->request->data)) {
            $this->RequestHandler->respondAs('json');
            $this->set("status", "success");
            $this->set("message", "All pages re-ordered");
            $this->set("_serialize", array("status", "message"));

My other action works fine with: 我的其他动作适用于:

$.ajax({
        url: "/btstadmin/pages/reorder.json",
        type: "post",
        dataType:"json",
        data: neworderSer,

my action which gives a json response 我的动作给出了json响应

public function reorder() {
    if ($this->Page->saveMany($data)) {
        $this->set("status", "sucess");
        $this->set("message", "All pages re-ordered");
        $this->set("content", $data);
        $this->set("_serialize", array("status", "message", "content"));

UPDATE UPDATE

I changed the form create 我更改了表单创建

$formaction = '/pages/ajax_edit/'.$this->data['Page']['id'].'.json';
echo $this->Form->create('Page', array('type' => 'file', 'url' => $formaction));

Your form's action url should be /btstadmin/pages/ajax_edit/3.json instead of /btstadmin/pages/ajax_edit.json/3 . 表单的操作网址应为/btstadmin/pages/ajax_edit/3.json而不是/btstadmin/pages/ajax_edit.json/3 The url extension should always be at the end of url. 网址扩展名应始终位于网址末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM