Front End Site API Test
Sometimes you want to build in custom functionality for front end users by passing info for those users based on their session and user id.
This was built by Mike to show this example A better example of a work case is ucanlearn.net where they allowed users to earn points by completing page activities and the points were stored to the user field so different pages could show scores, completion,etc.
The below field is saved and loaded using ajax into the platform for this user only via the site api public key. It uses the users session so it knows which user to reference for the field saving/loading.
The code that makes it work
<script>
$(function(){
$.ajax({
"url": "//www.lunawebs.com/site_api?ajax"
,"type": "post"
,"data": {
"pub_api_key": "104e3165db544552b1762856553a6137"
,"action": "user_field"
,"sysname": "field_api_test"
}
,"dataType": "json"
,"success": function(data) {
if ( data.success ) {
$('#the_form textarea').val(data.data);
} else {
alert(data.message);
}
}
});
$('#the_form button').click(function(){
$.ajax({
"url": "//www.lunawebs.com/site_api?ajax"
,"type": "post"
,"data": {
"pub_api_key": "104e3165db544552b1762856553a6137"
,"action": "user_field"
,"sysname": "field_api_test"
,"type": "set"
,"data": $('#the_form textarea').val()
}
,"dataType": "json"
,"success": function(data) {
if ( data.success ) {
alert('Saved!');
} else {
alert(data.message);
}
}
});
});
});
</script>