简体   繁体   English

CakePHP Ajax请求无法进入内部服务器错误

[英]CakePHP Ajax Request not going Internal Server Error

I am just trying to test ajax form submit in CakePhp. 我只是想在CakePhp中测试Ajax表单提交。 I have below written code. 我有下面的书面代码。 But ajax is not working. 但是ajax无法正常工作。

Error in chrome : POST http://localhost/john/Frontends/AjaxFormSubmit 500 (Internal Server Error) chrome中的错误: POST http://localhost/john/Frontends/AjaxFormSubmit 500(内部服务器错误)

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

Read your error log: 阅读您的错误日志:

Error: [MissingViewException] View file "G:\\wamp\\www\\john\\app\\View\\Frontends\\Ajax_Form_Submit.ctp" is missing. 错误:[MissingViewException]视图文件“ G:\\ wamp \\ www \\ john \\ app \\ View \\ Frontends \\ Ajax_Form_Submit.ctp”丢失。 Exception Attributes: array ( 'file' => 'G:\\wamp\\www\\john\\app\\View\\Frontends\\Ajax_Form_Submit.ctp', ) Request URL: /john/Frontends/AjaxFormSubmit 异常属性:数组('file'=>'G:\\ wamp \\ www \\ john \\ app \\ View \\ Frontends \\ Ajax_Form_Submit.ctp',)请求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 . 如果要返回json,请阅读本节

If you really want to just echo a string you need to call $this->_stop(); 如果您真的只想回显一个字符串,则需要调用$ 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. 但是实际上,除了返回无意义的字符串外,您还应该返回某种带有正确消息以及可能的错误代码和状态的json对象,以便您的JavaScript可以处理响应。

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

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