Code Snippets

  

JavaScript Source Code


Welcome to Dream.In.Code
Getting Help is Easy!

Join 105,772 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,458 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Ajax

Simple and flexible ajax You can use [GET]or [POST] method and you can Specify the function to process the response text

Submitted By: ahmad_511
Actions:
Rating:
Views: 954

Language: JavaScript

Last Modified: June 17, 2007
Instructions: call the Ajax_Send() function and pass the parameters Ex:
Ajax_Send("POST","www.Mysite.com/myurl","user=Ahmad&pass=1234&memtype=admin",myfunction)

function myfunction(response){
// code to process the response
}

Snippet


  1. // Here is The Ajax_Send Function
  2.  
  3. function Ajax_Send(GP,URL,PARAMETERS,RESPONSEFUNCTION){
  4. var xmlhttp //Declaring Variable
  5.  
  6. //Trying to catch xmlhttp
  7.  
  8. try{xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")}
  9. catch(e){
  10. try{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")}
  11. catch(e){
  12. try{xmlhttp=new XMLHttpRequest()}
  13. catch(e){
  14. alert("Your Browser Does Not Support AJAX")}}}
  15.  
  16. // Waiting the response text
  17. xmlhttp.onreadystatechange=function(){
  18. if (xmlhttp.readyState == 4)eval(RESPONSEFUNCTION(xmlhttp.responseText))// calling the function we want and passing the response text to it when ready
  19. }
  20.  
  21. // GET Method
  22. if (GP=="GET"){
  23. URL+="?"+PARAMETERS
  24. xmlhttp.open("GET",URL,true)
  25. xmlhttp.send(null)
  26. }
  27.  
  28. // POST Method
  29. if (GP="POST"){
  30. PARAMETERS=encodeURI(PARAMETERS)
  31. xmlhttp.open("POST",URL,true)
  32. xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
  33. xmlhttp.setRequestHeader("Content-length",PARAMETERS.length)
  34. xmlhttp.setRequestHeader("Connection", "close")
  35. xmlhttp.send(PARAMETERS)
  36. }
  37. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month