Welcome Guest, Not a member yet? Register   Sign In
Javascript fetch on a CI route returns 404
#1

I need to send an asynchronous request from my 'thank you' page back to my server so it can do some email sending without blocking the initial thank you response.  This all works fine in my local environment with WAMP.  I get back the text response from the controller serving that route.  However, executing the same code on my website server always returns a 404 to the fetch().
I excluded that route from the CSRF 'before' globals in Filter.php.
My controller function for the route:
PHP Code:
    public function ppStewardOnly()
    {
        $session session();
        $invoiceID $session->get('invoiceID');
        //  RSIMLogger("notice", 'Steward only invoice : ' . $invoiceID);
        $session->remove('invoiceID');

        $transactionsModel model($this->modelPrefix 'TransactionsModel');
        $transaction $transactionsModel->findInvoice("rpu_$invoiceID");
        //  RSIMLogger("notice", 'Transaction : ' . json_encode($transaction, JSON_PRETTY_PRINT));

        if ($transaction != null) {
            $json $transaction->getStewards();
            $stewards json_decode($jsontrue);
            //  RSIMLogger("notice", 'Stewards : ' . $json);

            $this->sendStewardNotificationEmails($stewards);
            RSIMLogger("notice"'sendStewardNotificationEmails : COMPLETE');
        }

        //  RSIMLogger("notice", 'ppStewardOnly : COMPLETE');

        header("Content-Type: application/json");
        header("Access-Control-Allow_Origin: *");

        echo (json_encode('NOTIFY Complete'));
    
My view for the 'thank you' page contains [with my non-jQuery version of documentReady()]:
Code:
<?php $this->section('pagescripts') ?>

<script>
function SendAsyncRequest() {
fetch("/checkout/stewardOnly", {
method: "GET",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
},
})
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
return response.json();
})
.then((data) => {
console.log(data);
})
.catch((error) => {
console.log(`Notification failed: ${error}`);
});
}

docReady(function() {
// alert('Testing async function');
SendAsyncRequest();
});
</script>

<?php $this->endSection() ?>
I can successfully execute a fetch() to a stand-alone php file which returns a simple json string.  But I can't get fetch() to give back anything but a 404 when the target of the fetch is a CI route.
When all that logging is enabled in the controller, nothing ever gets written to the log on my server, so I'm sure we never get to the controller.  All very frustrating since I can only really do debugging in the local environment where everything works fine.

Anyone have any ideas?
Thanks for the help.
Reply
#2

I believe you need to use the full url in the request:
Code:
fetch("https://YOURDOMAIN/yourdefinedroute", {
Reply




Theme © iAndrew 2016 - Forum software by © MyBB