Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Quick Light weight Ajax Function < 1kb

ajax

  • Please log in to reply
No replies to this topic

#1 f1shear

f1shear

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 13 March 2012 - 12:50 PM

/*

Ajax function

A light-weight ajax function built by me



Usage:



get_response(ajax_method, ajax_path, ajax_json_data, actionOnResponse)

***********************************************************************************

PARAMETERS

==========

ajax_method >>

GET or POST method.

	e.g. method="POST"





ajax_path >>

The URL of the file

	e.g. path='path/to/file_name'



ajax_json_data >>

The data to be sent to file via post or get method represented in JSON format.

	e.g. data={'user_id':'1111','message':'hello how are you'}



actionOnResponse >>

function to process response Text

	e.g.

	display_updated_post(response){

	//action to do on reponse text..

	}

If the process is in the form of XML then you have to change the code.



Overall example

===============

	get_response(method,path,data,display_updated_post)

Important

========

**GET requests are normally cached but this has been disabled by passing _request parameter.

**The code is free to use and edit.

**Please send us back the improved version.



Code written by

<<Pukchu Tim>>

Email:hackandlearn@gmail.com

Website: [url="http://www.hackandlearn.com"]Hack and Learn[/url]

***********************************************************************************



*/





temp_ajax_obj=new Array();



function get_response(ajax_method, ajax_path,ajax_json_data,actionOnResponse)

{

var ajax_data='';



for(var key in ajax_json_data)

{

value=ajax_json_data[key];

ajax_data=ajax_data+'&'+key+'='+encodeURIComponent(value);

}





var ajax_id=new Date().getTime(); //use Time in ms as Ajax_id.



//generate xmlhttp object

			if (window.XMLHttpRequest)

			  {// code for IE7+, Firefox, Chrome, Opera, Safari

			  temp_ajax_obj[ajax_id]=new XMLHttpRequest();

			 }

			else

			 {// code for IE6, IE5

			 temp_ajax_obj[ajax_id]=new ActiveXObject("Microsoft.XMLHTTP");

			   }

try

{

if(ajax_method=='POST')

{

temp_ajax_obj[ajax_id].open(ajax_method,ajax_path,true);

temp_ajax_obj[ajax_id].setRequestHeader("Content-type","application/x-www-form-urlencoded");

temp_ajax_obj[ajax_id].send('_request='+ajax_id+ajax_data);

}

else if(ajax_method=='GET')

{

temp_ajax_obj[ajax_id].open(ajax_method,ajax_path+'?_request='+ajax_id+ajax_data,true);

temp_ajax_obj[ajax_id].setRequestHeader("Content-type","application/x-www-form-urlencoded");

temp_ajax_obj[ajax_id].send();

}

}

catch(e)

{

alert("Unexpected error!");

}

			

			var ajax_response;

		

		temp_ajax_obj[ajax_id].onreadystatechange=function()

		{

		  if (temp_ajax_obj[ajax_id].readyState==4)

		   {

			 try{

				if((temp_ajax_obj[ajax_id].status>=200 &&

							   temp_ajax_obj[ajax_id].status<300) ||

							   temp_ajax_obj[ajax_id].status==304)

			   { // handle response

			   ajax_response=temp_ajax_obj[ajax_id].responseText;

			

			   //user defined function to handle ajax response

			   actionOnResponse(ajax_response);



			   //destroy the ajax object

			   temp_ajax_obj[ajax_id]=null;

			  }else{

				   alert( "Network Error: "+temp_ajax_obj[ajax_id].status+"  "+ temp_ajax_obj[ajax_id].statusText );

   				}

			  }catch(err){

			   // handle error

			  alert("Unexpected error!");

			  }

							

			}//end of if<readyState>

		}//end of onreadystatechange function

}//end of function get_response

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download