Showing posts with label Ajax. Show all posts
Showing posts with label Ajax. Show all posts

Friday, 25 June 2010

How to check session is exit or not using ajax call with jquery?

Hey fox please do the below mentioned code to check it out whether session is exist or not via ajax call:


Write down the following code while call ajax;

function callAjax()
    {
        $.ajax({       
               data: { fname: 'ashwani kumar' },             
              type: 'POST',
                  url: 'ajax.php',
                success: function(data) {
                    alert('success');
                    $('#content').html(data);           
                 },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                     //alert(XMLHttpRequest.status);
                    if (XMLHttpRequest.status === 500) {
                      location.href = 'index.php';
                    }
                  }
            });
    }

IN ajax.php file on top of the page write down the following code in your script:

if(!isset($_SESSION['name']))
{
    header("HTTP/1.0 500 Not Found");
    exit;
}


Cheers!

Friday, 21 May 2010

What are the differences b/w GET and POST in ajax call?

var http = new XMLHttpRequest();

Using GET method

Now we open a connection using the GET method.

var url = "getdata.php";
var params = "name=ashwani&action=true";
http.open("GET", url+"?"+params, true);
http.onreadystatechange = function() {//Call a function when the state changes.
 if(http.readyState == 4 && http.status == 200) {
  alert(http.responseText);
 }
}
http.send(null);
I really hope that this much is clear for you - I am assuming that you know a bit of Ajax coding.



POST method

We are going to make some modifications so POST method will be used when sending the request...

var url = "getdata.php";
var params = "name=ashwani&action=true";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
 if(http.readyState == 4 && http.status == 200) {
  alert(http.responseText);
 }
}
http.send(params);

The first change(and the most obvious one) is that I changed the first argument of the open function from GET to POST. Also notice the difference in the second argument - in the GET method, we send the parameters along with the url separated by a '?' character...

Cheers!

Friday, 9 April 2010

How to check Session is expire or not during Aajx Call?

As I told you in my previous post, there are some status code you found in during Ajax Call.
 
if(xmlhttp.status == 500) {
// That means session has been lost..
// Do your task whatever you want to do here... 
}
Cheers!

Difference b/w Synchronous and Asynchronous?

Suppose there are two process A and B are running and process B are depends upon the process A result.

If two processes are running Synchronously, it means that second process will wait until or unless first process complete.

If two processes are running Asynchronously, it means second process will not wait for second process result and will be executed.

Cheers!

Some Important Tips on Ajax

By default in a open method, you found something like this:
open("GET",url,false);

method: the type of request: GET or POST
url: the location of the file
boolean: true (asynchronous) or false (synchronous)

Here last parameter is false it means process will be synchronous.

To make process Asynchronous, make true instead of false.

Cheers!

What is the Status means in Ajax term?

Whenever response return it also return status.

for example:

if (xmlhttp.status == 200) // i.e., OK
if (xmlhttp.status == 404) // i.e., Page not found.
if (xmlhttp.status == 500) // i.e., Session expired.

Cheers!

What are the means of readyState in Ajax

There are four type of readyState exist in Ajax:

0: not initialized.
1: connection established.
2: request received.
3: answer in process.
4: finished. 
 
 
Cheers!