在codeigniter 4中,我已经使用jQuery发出了POST请求,但该请求未到达服务器

问题描述 投票:0回答:1

我正在将我的代码从CI3移植到CI4。已在CI3中使用jQuery的AJAX POST请求未到达服务器代码。


这是客户端代码

$.ajax({
    url: 'recordCreate/' + serialCode,
    type: 'POST',
    data: {data: data},
    dataType : 'text',
}).done(function(result) {
    alert(result);
});

我的路线

$routes->post('recordCreate/(:alpha)', 'AjaxWrite::recordCreate/$1');

控制器是这个

<?php namespace App\Controllers;
use CodeIgniter\Controller;
class AjaxWrite extends Controller{
  public function __construct()
  {
  }
  function recordCreate($serailCode)
  {
    echo urldecode($serailCode);
    echo $this->request->getPost('data');
  }
}

并且包含在名为'AjaxWrite.php'的文件中

有了这个,我得到了错误

找不到控制器或其方法:App \ Controllers \ RecordCreate :: qweq

其中'qweq'是我在URL中传递的serialCode的值。

jquery post get codeigniter-3 codeigniter-4
1个回答
1
投票

CodeIgniter 4是对框架的重写,并且不向后兼容。从到目前为止的经验来看,将应用程序从版本3移植到版本4似乎也正在重写该应用程序。

文档说... It is more appropriate to think of converting your app, rather than upgrading it

在版本3中,无论客户端代码如何发出请求,路由都是相同的。在版本4中,您的路线应反映您对客户的意图。

我使用CI4的第一个AJAX请求是GET。当我第一次使用POST请求时,我将请求通过隧道传输到控制器$routes->get()这是请求未到达控制器的原因。从评论部分,您将看到@timbrownlaw(可能对CI很有经验)将其视为问题所在。

因此解决方案是jQuery(客户端)POST请求应通过]

$routes->post()

出现在问题代码中的路线。在@timbrownlaw帮助之前,路线是

$routes->get()

您可以看到more from the documentation

© www.soinside.com 2019 - 2024. All rights reserved.