Ajax call to REST service
This article gives you instructions on how to use ajax call to REST service in Kentico CMS.
I will not discuss all possible types of requests in REST, I will focus only on
POST request as the code for other types would be very similar. First step is to make Ajax work on your site, which can be accomplished via the
CMS Desk on your root page, then to the
Master Page tab, and add the code below to the
HEAD tag:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
So right now Ajax is working on your site and you can simply add the following code to your page:
<script type="text/javascript">
function formToJSON() {
return JSON.stringify({"NodeClassID":3432,"DocumentPageTemplateID":41,"DocumentName":"JSONTest"});
}
$(function() {
$.ajax({
type: 'POST',
contentType: 'application/json',
url: 'http://localhost/KenticoCMS_7_newest/rest/content/site/corporatesite/en-us/document/Home',
data: formToJSON(),
username: 'administrator',
password: 'roman',
success: function(data, textStatus, jqXHR){ alert('Stock updated successfully. Status: '+textStatus); },
error: function(jqXHR, textStatus, errorThrown){ alert('update Stock error: ' + textStatus); }
});
});
</script>
The Ajax call above will create a new child document of your Home page called "JSONTest". If you wonder where I got the IDs for my REST query, you can find NodeClassID in CMS_Tree database table and DocumentName in CMS_Document table.