MiX1.TK
Site menu
Login form
Section categories
Web Development [20]
Learn about PHP & Web Development
Education [4]
Learn for Free
Knowledge & Tricks [0]
General Knowledge,computer Knowledge & tricks
Writing World [2]
A MiX World of SmS on Various Topics
Search
Main » 2012 » November » 6 » Detect the Ajax Request using php code
10:15 PM
Detect the Ajax Request using php code
how to detect the Http Request that it is a simple post ot ajax post


<?php
 $isXmlHttpRequest = array_key_exists('X_REQUESTED_WITH', $_SERVER) &&
 $_SERVER['X_REQUESTED_WITH'] == 'XMLHttpRequest';
 
 if ($isXmlHttpRequest) {
 // is an Ajax request
 }
 else {
 // is not an Ajax request
 }
?>
 
 
and if you want to return json data to the ajax request thencode will be

Outputting JSON data for Ajax requests only 
 
 <?php
 $isXmlHttpRequest = array_key_exists('X_REQUESTED_WITH', $_SERVER) &&
 $_SERVER['X_REQUESTED_WITH'] == 'XMLHttpRequest';
 
 if ($isXmlHttpRequest) {
 $data = array(
 'foo' => 'bar'
 );
 
 header('Content-type: application/json');
 echo json_encode($data);
 exit;
 }
?>
Category: Web Development | Views: 347 | Added by: admin-priyank | Rating: 5.0/1
Total comments: 0
Name *:
Email *:
Code *:
Calendar
«  November 2012  »
SuMoTuWeThFrSa
    123
45678910
11121314151617
18192021222324
252627282930
Entries archive
Our poll
Rate my site
Total of answers: 4
Site friends
  • Create a free website
  • Online Desktop
  • Free Online Games
  • Video Tutorials
  • All HTML Tags
  • Browser Kits
  • Statistics

    Total online: 1
    Guests: 1
    Users: 0
    Creative Commons License
    Free Downloads and Services by MiX1 is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
    Copyright MyCorp © 2024