Last active
May 19, 2022 03:48
-
-
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..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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