You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter4密码重置系统AJAX请求路径错误问题求助

Hey there, let's work through this AJAX routing issue in your CodeIgniter 4 password reset flow! It sounds like the core problem is that your AJAX request is using a relative path instead of an absolute one, causing it to append to the current page's URL (the tokenized reset link from your email) rather than hitting the intended /resetpassword route. Here's how to fix it:

Step 1: Fix the AJAX Request URL in Your View

The most likely culprit is that your AJAX code is using a relative path for the url parameter. When the user is on http://loginregister.test/redirect/38/resetpassword, a relative path like resetpassword will resolve to http://loginregister.test/redirect/38/resetpassword instead of the root-level route.

Update your AJAX code to use CodeIgniter's base_url() helper to generate an absolute path:

$.ajax({
  url: '<?= base_url('resetpassword') ?>', // Generates the full root URL for your route
  type: 'POST',
  data: $('#your-reset-form-id').serialize(),
  // ... rest of your AJAX logic (success/error handlers, etc.)
});

Alternatively, if your form has an action attribute, make sure that also uses an absolute path:

<form id="your-reset-form-id" action="<?= base_url('resetpassword') ?>" method="post">
  <!-- Your form fields here -->
</form>

Step 2: Verify Your Route Configuration

Double-check your route.php to ensure you have a clear POST route mapped to your reset password handler. It should look something like this:

// Make sure this route exists and points to the correct controller method
$routes->post('resetpassword', 'ResetPwdController::processReset');

Replace processReset with the actual method name in your ResetPwdController that handles the password reset submission.

Step 3: (Optional) Use CI4's View Return Best Practice

While this isn't directly causing your URL issue, CodeIgniter 4 recommends returning views instead of echoing them for better framework compatibility. If you're currently using echo view('path/to/reset-form');, switch to:

public function showResetForm($token) {
  // Your token validation logic here
  return view('auth/reset_password'); // Return the view instead of echoing
}

This won't fix the AJAX path problem, but it keeps your code aligned with CI4's standards.

Why This Works

By using base_url(), you're ensuring the AJAX request targets the root-level /resetpassword route regardless of the current page's URL. The relative path was the main issue—your browser was simply resolving the request relative to the tokenized link the user clicked from their email.

内容的提问来源于stack exchange,提问作者Ali Tenni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 14:44:04