Table API JavaScript examples

Examples that demonstrate how to use the Table API with the JavaScript language.

JavaScript Examples

Note: The REST API does not support Cross-Origin requests. AJAX responses coming from different ServiceNow domains are not allowed by browsers since the response will be missing headers to support cross-domain requests. The samples provided below demonstrate sending AJAX requests only from the same domain. You can use the following scripts on UI pages in the ServiceNow domain.

GET

  function getAction(){
 	var client=new XMLHttpRequest(); 
 	client.open("GET","/api/now/table/incident"); 
 	client.setRequestHeader('Accept','application/json');
 	client.onreadystatechange = function(){
 		if(this.readyState == this.DONE){
 			document.getElementById("response").innerHTML=this.status + this.response;
 		}};
 		client.send();
 	}

POST

function postAction(){
 		var requestBody = '{"short_description":"Test insert"}';
 
 		var client=new XMLHttpRequest();
 		client.open("POST","/api/now/table/incident");
 		client.setRequestHeader('Accept','application/json');
 		client.setRequestHeader('Content-Type','application/json');
 		client.onreadystatechange = function(){
 			if(this.readyState == this.DONE){
 				document.getElementById("response").innerHTML=this.status + this.response;
 			}};
 
 			client.send(requestBody);
 		}

PUT

   function putAction(){
 		var requestBody = '{"short_description":"Test update"}';
 
 		var client=new XMLHttpRequest();
 		client.open("PUT","/api/now/table/incident/70a503b1f1971100a92e33bc2fbe836b");
 		client.setRequestHeader('Accept','application/json');
 		client.setRequestHeader('Content-Type','application/json');
 		client.onreadystatechange = function(){
 			if(this.readyState == this.DONE){
 				document.getElementById("response").innerHTML=this.status + this.response;
 			}};
 			client.send(requestBody);	
 		}

PATCH

function patchAction(){
 			var requestBody = '{"short_description":"Test update with Patch"}';
 
 			var client=new XMLHttpRequest();
 			client.open("PATCH","/api/now/table/incident/70a503b1f1971100a92e33bc2fbe836b");
 			client.setRequestHeader('Accept','application/json');
 			client.setRequestHeader('Content-Type','application/json');
 			client.onreadystatechange = function(){
 				if(this.readyState == this.DONE){
 					document.getElementById("response").innerHTML=this.status + this.response;
 				}}
 				client.send(requestBody);
 			}

DELETE

 function deleteAction(){
 				var client=new XMLHttpRequest();
 				client.open("DELETE","/api/now/table/incident/70a503b1f1971100a92e33bc2fbe836b");
 				client.setRequestHeader('Accept','application/json');
 				client.setRequestHeader('Content-Type','application/json');
 				client.onreadystatechange = function(){
 					if(this.readyState == this.DONE){
 						document.getElementById("response").innerHTML=this.status + this.response;
 					}}
 					client.send();
 				}