【发布时间】:2020-07-14 08:46:52
【问题描述】:
我正在将我的代码从 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 的值。
【问题讨论】:
-
P;请轻松上传您的代码,以便其他人可以帮助您。
-
检查您的路线。您可能需要为您的 AJAX URL 定义一个 $routes->post()。
-
感谢@timbrownlaw 提供此信息。我对所有路线都使用了 $routes->get() 。它必须与路线有关。现在使用 $routes->post(),我收到错误
Controller or its method is not found: App\Controllers\ControllerMethod::parameter -
您能否将您的路线添加到您的问题中。你所描述的看起来不对。
-
非常感谢@timbrownlaw。为什么在将路线从
$routes->get()更改为$routes->post()后仍然出现错误,老实说,我不知道。你告诉我的是正确的 - 需要为你的 AJAX URL 定义一个 $routes->post()。因此,代码现在可以正常工作,因为它现在出现在问题上。非常感谢。
标签: jquery post get codeigniter-3 codeigniter-4