What's Here?
- Members: 105,772
- Replies: 404,497
- Topics: 61,347
- Snippets: 2,214
- Tutorials: 597
- Total Online: 1,458
- Members: 66
- Guests: 1,392
Who's Online?
|
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!
|
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
|
|
|
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
// Here is The Ajax_Send Function
function Ajax_Send(GP,URL,PARAMETERS,RESPONSEFUNCTION){
var xmlhttp //Declaring Variable
//Trying to catch xmlhttp
try{xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")}
catch(e){
try{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")}
catch(e){
try{xmlhttp=new XMLHttpRequest()}
catch(e){
alert("Your Browser Does Not Support AJAX")}}}
// Waiting the response text
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState == 4)eval(RESPONSEFUNCTION(xmlhttp.responseText))// calling the function we want and passing the response text to it when ready
}
// GET Method
if (GP=="GET"){
URL+="?"+PARAMETERS
xmlhttp.open("GET",URL,true)
xmlhttp.send(null)
}
// POST Method
if (GP="POST"){
PARAMETERS=encodeURI(PARAMETERS)
xmlhttp.open("POST",URL,true)
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlhttp.setRequestHeader("Content-length",PARAMETERS.length)
xmlhttp.setRequestHeader("Connection", "close")
xmlhttp.send(PARAMETERS)
}
}
Copy & Paste
|
|
|
Programming
Web Development
Reference Sheets
Bye Bye Ads
Free DIC T-Shirt
Related Sites
Monthly Drawing
Partners
Top Contributors
Top 10 Kudos This Month
|