Skip to content

Instantly share code, notes, and snippets.

@binarykore
Last active May 19, 2022 03:48
Show Gist options
  • Select an option

  • Save binarykore/82d9522e798418a3784db2efcebd5b06 to your computer and use it in GitHub Desktop.

Select an option

Save binarykore/82d9522e798418a3784db2efcebd5b06 to your computer and use it in GitHub Desktop.
Difference between GET and POST as well as Asynchronous and Synchronous Ajax / XMLHTTPRequest..
<?php
Header("Content-Type:Text/Javascript");
$_requests = ["POST"=>[],"GET"=>[]];
$_requests["POST"]["ASYNCH"] = ("var xhr = new XMLHttpRequest();
xhr.open('POST','//".$_SERVER["HTTP_HOST"]."/handler',true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var result = xhr.responseText;
}
}
xhr.send('data=hello');");
$_requests["POST"]["SYNCH"] = ("var xhr = new XMLHttpRequest();
xhr.open('POST','//".$_SERVER["HTTP_HOST"]."/handler',false);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var result = xhr.responseText;
}
}
xhr.send('data=hello');");
$_requests["GET"]["ASYNCH"] = ("var xhr = new XMLHttpRequest();
xhr.open('GET','//".$_SERVER["HTTP_HOST"]."/handler',true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var result = xhr.responseText;
}
}
xhr.send(null);");
$_requests["GET"]["SYNCH"] = ("var xhr = new XMLHttpRequest();
xhr.open('GET','//".$_SERVER["HTTP_HOST"]."/handler',false);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var result = xhr.responseText;
}
}
xhr.send(null);");
print_r($_requests);
//True for Asynchronous
//False for Synchronous
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment