Welcome Guest, Not a member yet? Register   Sign In
Simple POST request - not getting to controller?
#1

Is there some setting I'm missing to get a post request to go to the correct controller?
I've tried to isolate the issues with a simple test controller and view, but somehow, the POST request appears to get redirected elsewhere to a fully different controller. What would cause this?

The view, test.php:
Code:
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Post test</title>
  </head>
  <body>
    <h2>Testing XMLHttpRequest Object</h2>
    <button type="button" onclick="loadDoc()">Request data</button>

    <p id="demo"></p>

    <script>
    function loadDoc() {
      const xhttp = new XMLHttpRequest();
      xhttp.onload = function() {
        console.log(this);
        var jsonObj = JSON.parse(this.responseText);
        document.getElementById("demo").innerHTML = jsonObj.response;
      }
      xhttp.open("POST", "<?= base_url('test/detail'); ?>", true);
      xhttp.setRequestHeader("Content-type", "application/json");
      var data = {uid:1234};
      xhttp.send(JSON.stringify(data));
    }
    </script>

 
  </body>
</html>

The controller:
PHP Code:
<?php

namespace App\Controllers;

class 
Test extends BaseController {

    public function index() {
        echo view('test');
    }
    
    
public function detail() {
        //DEBUG
        $info = [
            'method'  => $this->request->getMethod(),
        ];
        log_message('debug''In test/detail, method={method}'$info);
        
        $response 
= [ 
          "response"  => "Working"
        ];
        
        
return $this->response->setJSON($response);
    }


Routes.php:
PHP Code:
<?php

namespace Config;

// Create a new instance of our RouteCollection class.
$routes Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (file_exists(SYSTEMPATH 'Config/Routes.php')) {
    require SYSTEMPATH 'Config/Routes.php';
}

/*
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);

/*
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
 
$routes->post('test/detail''Test::detail');


/*
 * --------------------------------------------------------------------
 * Additional Routing
 * --------------------------------------------------------------------
 *
 * There will often be times that you need additional routing and you
 * need it to be able to override any defaults in this file. Environment
 * based routes is one such time. require() additional route files here
 * to make that happen.
 *
 * You will have access to the $routes object within that file without
 * needing to reload it.
 */
if (file_exists(APPPATH 'Config/' ENVIRONMENT '/Routes.php')) {
    require APPPATH 'Config/' ENVIRONMENT '/Routes.php';


Apache log - note the last line where it appears to redirect to another controller in the app:
Code:
127.0.0.1 - - [08/Dec/2021:13:01:42 -0500] "GET /test HTTP/1.1" 200 800
127.0.0.1 - - [08/Dec/2021:13:02:12 -0500] "POST /test/detail HTTP/1.1" 303 411
127.0.0.1 - - [08/Dec/2021:13:02:12 -0500] "GET /index.php/ride/list HTTP/1.1" 200 9781

Codeigniter log - never gets to the detail function, as you'd see a DEBUG line here:
Code:
INFO - 2021-12-08 13:02:12 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.
INFO - 2021-12-08 13:02:12 --> Session: Class initialized using 'CodeIgniter\Session\Handlers\FileHandler' driver.

Finally, .htaccess, which I don't believe I've modified:
Code:
# Disable directory browsing
Options All -Indexes

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On

# If you installed CodeIgniter in a subfolder, you will need to
# change the following line to match the subfolder you need.
# http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
# RewriteBase /

# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to the front controller, index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]

# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 index.php
</IfModule>

# Disable server signature start
ServerSignature Off
# Disable server signature end

Any ideas?
Reply
#2

Solved it after posting: CSRF was getting in the way.  Implementing in view and controller resolved it.  See examples below for future reference (showing both POST and GET methods).
view:
Code:
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

 
    <title>Post test</title>
  </head>
  <body>
     
    <input type="hidden" id="csrf_txt" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>" />


    <h2>Testing XMLHttpRequest Object - Post</h2>
    <button type="button" onclick="loadDoc()">Request data</button>

    <p id="demo"></p>
   
    <h2>Testing XMLHttpRequest Object - Get</h2>
    <button type="button" onclick="loadDocGet()">Request data</button>

    <p id="demo_get"></p>

    <script>
    function loadDoc() {
      const xhttp = new XMLHttpRequest();
     
      // CSRF Hash
      var csrf = document.getElementById("csrf_txt");
      var csrfName = csrf.getAttribute("name");  // CSRF Token Name
      var csrfHash = csrf.getAttribute("value"); // CSRF Hash
     
      xhttp.onload = function() {
        console.log(this);
        var jsonObj = JSON.parse(this.responseText);
        // Update CSRF hash
        document.getElementById("csrf_txt").setAttribute("value", jsonObj.token);
        document.getElementById("demo").innerHTML = jsonObj.response;
      }
      xhttp.open("POST", "<?= base_url('test/detail'); ?>", true);
      xhttp.setRequestHeader("Content-type", "application/json");
      var data = {uid:1234,[csrfName]:csrfHash};
      xhttp.send(JSON.stringify(data));
    }
    function loadDocGet() {
      const xhttp = new XMLHttpRequest();
      xhttp.onload = function() {
        console.log(this);
        var jsonObj = JSON.parse(this.responseText);
        document.getElementById("demo_get").innerHTML = jsonObj.response;
      }
      xhttp.open("GET", "<?= base_url('test/detail?uid=4321'); ?>", true);
      xhttp.send();
    }
   
   
    </script>

 
  </body>
</html>


And Controller:
PHP Code:
<?php

namespace App\Controllers;

class 
Test extends BaseController {

    public function index() {
        echo view('test');
    }
    
    
public function detail() {
        //DEBUG
        $info = [
            'method'  => $this->request->getMethod(),
            'uid'      => $this->request->getVar('uid'),
        ];
        log_message('debug''In test/detail, method={method}, uid={uid}'$info);
      
        $response 
= [ 
          "response"  => "Working - ".$this->request->getVar('uid'), 
          "token"      => csrf_hash(),
        ];
        
        
return $this->response->setJSON($response);
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB