• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"isAJAX()" method always return false;

#1
I tried to create an API with a script like the one below. when I run it with $.ajax , the application always returns the "false" value in the isAJAX method. Whats wrong?
Thanks before.

PHP Code:
<?php 
namespace App\Controllers\Api;

use function 
time;
use 
CodeIgniter\API\ResponseTrait;
use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\ResponseInterface;

final class 
Test extends Controller 
{
 
   use ResponseTrait;

 
   public function index(): ResponseInterface
    
{
 
       return $this->respond([
 
           'ack' => time(),
 
           'req' => $this->request->getPost('foo'),
 
           'head' => $this->request->getServer('HTTP_BEARER'),
 
           'ajax' => $this->request->isAJAX(),
 
           ]);
 
   }

Reply

#2
(09-29-2018, 07:41 AM)titounnes Wrote: I tried to create an API with a script like the one below. when I run it with $.ajax , the application always returns the "false" value in the isAJAX method. Whats wrong?
Thanks before.

PHP Code:
<?php 
namespace App\Controllers\Api;

use function 
time;
use 
CodeIgniter\API\ResponseTrait;
use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\ResponseInterface;

final class 
Test extends Controller 
{
 
   use ResponseTrait;

 
   public function index(): ResponseInterface
    
{
 
       return $this->respond([
 
           'ack' => time(),
 
           'req' => $this->request->getPost('foo'),
 
           'head' => $this->request->getServer('HTTP_BEARER'),
 
           'ajax' => $this->request->isAJAX(),
 
           ]);
 
   }


In my case all works fine.

Header on welcome_message.php
Code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
        <script>
           jQuery(document).ready(function () {
               $.ajax({
                   url: '/ajax-html',
                   method: 'GET',
                   success: function (data) {
                       console.log(data);
                   }
               })
           })
</script>

Routes.php
PHP Code:
$routes->get('/ajax-html''Home::ajaxOrHtml'); 

Controller Home.php
PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\API\ResponseTrait;
use 
CodeIgniter\Controller;

class 
Home extends Controller
{
 
   use ResponseTrait;

    public function 
ajaxOrHtml () {
     
   if ($this->request->isAJAX()) {
     
       return $this->respond([
     
           'isAjax' => $this->request->isAJAX()
 
           ]);
 
       }
 
       return view('ajax_view');
 
   }


[Image: xMpOHDo.png]
Reply

#3
(09-29-2018, 11:32 AM)unodepiera Wrote:
(09-29-2018, 07:41 AM)titounnes Wrote: I tried to create an API with a script like the one below. when I run it with $.ajax , the application always returns the "false" value in the isAJAX method. Whats wrong?
Thanks before.

PHP Code:
<?php 
namespace App\Controllers\Api;

use function 
time;
use 
CodeIgniter\API\ResponseTrait;
use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\ResponseInterface;

final class 
Test extends Controller 
{
 
   use ResponseTrait;

 
   public function index(): ResponseInterface
    
{
 
       return $this->respond([
 
           'ack' => time(),
 
           'req' => $this->request->getPost('foo'),
 
           'head' => $this->request->getServer('HTTP_BEARER'),
 
           'ajax' => $this->request->isAJAX(),
 
           ]);
 
   }


In my case all works fine.

Header on welcome_message.php
Code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
        <script>
           jQuery(document).ready(function () {
               $.ajax({
                   url: '/ajax-html',
                   method: 'GET',
                   success: function (data) {
                       console.log(data);
                   }
               })
           })
</script>

Routes.php
PHP Code:
$routes->get('/ajax-html''Home::ajaxOrHtml'); 

Controller Home.php
PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\API\ResponseTrait;
use 
CodeIgniter\Controller;

class 
Home extends Controller
{
 
   use ResponseTrait;

    public function 
ajaxOrHtml () {
     
   if ($this->request->isAJAX()) {
     
       return $this->respond([
     
           'isAjax' => $this->request->isAJAX()
 
           ]);
 
       }
 
       return view('ajax_view');
 
   }


[Image: xMpOHDo.png]

Thanks for your reply. Solve with add  "X-Requested-With": 'XMLHTTPRequest' in header.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.