Friday, January 11, 2013

AJAX - perl


CALLING SERVER SIDE PERL SCRIPT THROUGH AJAX

Create an XMLHttpRequest Object:
      
      var xmlhttp;
      if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }
      else
     {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }

USING GET METHOD:

xmlhttp.open("GET","demo_get.pl",true);
xmlhttp.send();

OR

xmlhttp.open("GET","demo_get2.asp?fname=Henry&lname=Ford",true);
xmlhttp.send();

USING POST METHOD:
xmlhttp.open("POST","demo_get.pl",true);
xmlhttp.send();

OR

xmlhttp.open("POST","demo_get2.asp?fname=Henry&lname=Ford",true);
xmlhttp.send();

Server Response:
xmlhttp.responseText //get the response data as a string

xmlhttp.responseXML  //get the response data as XML data

The onreadystatechange event:

When readyState is 4 and status is 200, the response is ready:

xmlhttp.onreadystatechange=function()

  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    here_is_response=xmlhttp.responseText;
    }
  }

  The above function is automatically called when the status of response is changed. Specifically used while AJAX, which is TRUE value in caller.

While getting the response for Synchronous call, which is FALSE value in caller, we use it straight forward:

 xmlhttp.open("GET","demo_get.pl?resultFileName="+resultFileName,false);
 xmlhttp.send();
 reply=xmlhttp.responseText; 

here, server side script can by anything: php, perl, asp, jsp, etc







No comments:

Post a Comment