duanmu8911 2013-08-13 13:37
浏览 88
已采纳

CakePHP Ajax请求不会发生内部服务器错误

I am just trying to test ajax form submit in CakePhp. I have below written code. But ajax is not working.

Error in chrome : POST http://localhost/john/Frontends/AjaxFormSubmit 500 (Internal Server Error)

View File

 echo $this->Form->input('appendedInputButton',
         array('id'=>'url','name' => 'AjaxFormSubmit', 'type' => 'text')); 

 echo $this->Form->button('Convert',
         array('id' => 'Convert', 'type' => 'button'));

jQuery

$(document).ready(function () {

    $('#Convert').on({
        'click': function () {
            var urlVal = $("#url").val();
            if (urlVal == '' || urlVal == 0) {
                $("#url").focus();
                return false;
            }

            $.ajax({
                url: 'http://localhost/john/Frontends/AjaxFormSubmit',
                cache: false,
                type: 'POST',
                success: function (data) {
                    alert(data);
                }
            });
        }
    });
});

Controller

App::uses('AppController', 'Controller');

class FrontendsController extends AppController {

    public $name = 'Frontends';
    public $uses = array();
    public $components = array('RequestHandler');
    public $helpers = array('Html', 'Form');


    public function index() {
        $this - > layout = 'frontend_index_layout';
    }

    public function AjaxFormSubmit() {
        echo "Receiving Via Ajax";
    }
}

Error Log File

2013-08-13 13:41:43 Error: [MissingControllerException] Controller class JsController could not be found.
Exception Attributes: array (
  'class' => 'JsController',
  'plugin' => NULL,
)
Request URL: /john/js/jquery-1.10.1.min.map
Stack Trace:
#0 G:\wamp\www\john\app\webroot\index.php(111): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#1 {main}
2013-08-13 13:42:01 Error: [MissingViewException] View file "G:\wamp\www\john\app\View\Frontends\Ajax_Form_Submit.ctp" is missing.
Exception Attributes: array (
  'file' => 'G:\\wamp\\www\\john\\app\\View\\Frontends\\Ajax_Form_Submit.ctp',
)
Request URL: /john/Frontends/AjaxFormSubmit
Stack Trace:
#0 G:\wamp\www\john\lib\Cake\View\View.php(468): View->_getViewFileName(NULL)
#1 G:\wamp\www\john\lib\Cake\Controller\Controller.php(948): View->render(NULL, NULL)
#2 G:\wamp\www\john\lib\Cake\Routing\Dispatcher.php(194): Controller->render()
#3 G:\wamp\www\john\lib\Cake\Routing\Dispatcher.php(162): Dispatcher->_invoke(Object(FrontendsController), Object(CakeRequest), Object(CakeResponse))
#4 G:\wamp\www\john\app\webroot\index.php(111): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#5 {main}
  • 写回答

1条回答 默认 最新

  • douren0558 2013-08-13 13:54
    关注

    Read your error log:

    Error: [MissingViewException] View file "G:\wamp\www\john\app\View\Frontends\Ajax_Form_Submit.ctp" is missing. Exception Attributes: array ( 'file' => 'G:\wamp\www\john\app\View\Frontends\Ajax_Form_Submit.ctp', ) Request URL: /john/Frontends/AjaxFormSubmit

    The issue is pretty clear: Your view file is missing, so create it. I don't know what kind of answer you expect from it but what you do is wrong. If you want to return json read this section.

    If you really want to just echo a string you need to call $this->_stop(); after the echo. But instead of returning a meaningless string you should in fact return some kind of json object with a proper message and maybe error code and status so that the response can be handled by your javascript.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵