EllisLab text mark
Advanced Search
1 of 2
1
   
How to detect a Ajax request?
Posted: 26 August 2007 01:19 PM
Joined: 2007-08-26
4 posts

Hello,

I’d like to use Ajax (surely Mootools) on my website, so I need a function to detect whether a request is from Ajax or not, because I want not to send the same data in each case.
Do you know how to do that?

Thank you

 
Posted: 26 August 2007 01:56 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-05
53 posts
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest"{
        }else{
            
// header("Location: " . base_url());
        


this is how i do it.

 
Posted: 26 August 2007 03:55 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2006-06-23
370 posts

Doosje, that’s a very handy approach. Has it been your experience that that works with all Apache installs?

 Signature 

Mac OS X 10.8, Apache 2.x, NGiNX, PHP 5.4.x, CodeIgniter 1.7.2/2.1.3

 
Posted: 26 August 2007 04:15 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-05
53 posts

You can just try it with vardump($_SERVER) and see if the ‘extra’ XMLHttpRequest is there.
If you use prototype & scriptaculous .. this works…. I don’t know if you’d make your own library but i guess it works too.

 
Posted: 26 August 2007 06:33 PM   [ # 4 ]   [ Rating: 0 ]
Joined: 2007-08-26
4 posts

I’m sure there’s a better way than using vardump… That looks like quite ugly, doesn’t it?

 
Posted: 26 August 2007 06:42 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-05
53 posts

it’s just for checking if the extra header “XMLHttpRequest” is there. .. nothing else..
You could do a echo $_SERVER[‘XMLHttpRequest’]; but ... if it’s called someting else, you could miss it. !

 
Posted: 26 August 2007 06:50 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2007-08-26
4 posts

I want to use Mootool, so I’ve just to find the name for the variable (XMLHttpRequest), test it and it’s ok, isn’t it?

 
Posted: 26 August 2007 06:52 PM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-05
53 posts

It sure looks like it..grin

try my code ...and see what it does.

 
Posted: 27 August 2007 09:09 AM   [ # 8 ]   [ Rating: 0 ]
Joined: 2007-07-24
82 posts

The code Doojse posted has worked for me in the past, didn’t do any major compatibility tests though.

 
Posted: 27 August 2007 10:43 AM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2007-05-30
46 posts

This code works with all major javascript frameworks, and it’s easy to include in any ajax library, you only need to send this header with any ajax call.

I’ve extended the input library to add a function that return TRUE, FALSE,

function isAjax() {
    
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest");

in this way we can check ajax request with

if($this->input->isAjax()) {
        
...
    
 Signature 

Alex Sancho
Personal Weblog
Web Studio

 
Posted: 09 January 2009 12:01 PM   [ # 10 ]   [ Rating: 0 ]
Joined: 2008-01-02
66 posts

Thanks, this is working good!

It could be good having something like

$this->load->ajax(‘page’)

instead of

//Check ajax here
$this->load->view('page'
 
Posted: 19 September 2011 03:49 AM   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-29
77 posts
Ignacio - 09 January 2009 05:01 PM

Thanks, this is working good!

It could be good having something like

$this->load->ajax(‘page’)

instead of

//Check ajax here
$this->load->view('page'

or you could do

if($this->input->isAjax()) {
    $this
->load->view('my_ajax_view');
 Signature 

Junior PHP dev looking for a project to get started with.

 
Posted: 19 September 2011 04:51 AM   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2010-09-17
606 posts
atno - 19 September 2011 07:49 AM
if($this->input->isAjax()) {
    $this
->load->view('my_ajax_view');

Well the topic is several years old :)

 Signature 

I love lasagne!

 
Posted: 19 September 2011 04:55 AM   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-29
77 posts

Bad me!
BTW, I love lasagne too smile

 Signature 

Junior PHP dev looking for a project to get started with.

 
Posted: 19 September 2011 06:02 AM   [ # 14 ]   [ Rating: 1 ]
Joined: 2009-07-02
34 posts

Is User Guide:

$this->input->is_ajax_request() 

http://ellislab.com/codeigniter/user-guide/libraries/input.html

 Signature 

Webcoding.pl - Tworzenie stron WWW

 
Posted: 19 September 2011 06:04 AM   [ # 15 ]   [ Rating: 0 ]
Avatar
Joined: 2010-09-17
606 posts
Piter - 19 September 2011 10:02 AM

Is User Guide:

$this->input->is_ajax_request() 

http://ellislab.com/codeigniter/user-guide/libraries/input.html

Welcome to year 2007!
That’s what happens when you bump old posts atno :P

 Signature 

I love lasagne!

 
1 of 2
1