Last active
April 23, 2020 22:39
-
-
Save antic183/981b79c5b2e8a28a2d5d14c92c989c3c to your computer and use it in GitHub Desktop.
php low level rest example
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 | |
//print_r($_SERVER['REQUEST_METHOD']); | |
switch($_SERVER['REQUEST_METHOD']) { | |
case 'GET': | |
echo 'GET'; // do anything | |
break; | |
case 'POST': | |
echo 'POST'; // do anything | |
break; | |
case 'PUT': | |
echo 'PUT'; // do anything | |
break; | |
case 'DELETE': | |
echo 'DELETE'; // do anything | |
break; | |
default: | |
echo 'undefined request type!'; | |
} | |
?> |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Low level rest example</title> | |
<script src="./bower_components/jquery/dist/jquery.min.js"></script> | |
</head> | |
<body> | |
<button id="get">GET request</button> | |
<button id="post">POST request</button> | |
<button id="put">PUT request</button> | |
<button id="delete">DELETE request</button> | |
<script> | |
$(document).ready(function() { | |
var requestTypes = ["get", "post", "put", "delete"]; | |
$.each(requestTypes, function( index, value ) { | |
$('#' + value).on('click', function() { | |
request = triggerRequest(value); | |
}); | |
}); | |
function triggerRequest(requestType) { | |
if ($.inArray(requestType, requestTypes) !== -1) { | |
var data = {firstname: "John", lastname: "doe"}; | |
return $.ajax({ | |
method: requestType, | |
url: "rest-api.php", | |
cache: false, | |
async: true, | |
data: data | |
}).done(function(msg) { | |
alert("request methode = " + msg); | |
}).fail(function( jqXHR, textStatus ) { | |
console.error( "Request failed: "); | |
console.error( textStatus ); | |
}); | |
} | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment