JSON Method

I came across a scenario in my work place where I need to use client script WebService REST message to call a MID server to get result from the actual server which is behind the firewall. However, this time I will just share how I use client script to get the results and assigned them to object using JSON (JavaScript Object Notation) method in Servicenow.

try {
		 var r = new sn_ws.RESTMessageV2('NAT DNS API', 'dns_zone');

		 var response = r.execute();
		 var responseBody = response.getBody() ;
		var obj = JSON.parse(responseBody);
		obj.message='Message';
		obj.httpStatus = response.getStatusCode();
		var oj = JSON.stringify(obj);

		 gs.setRedirect("x_person.do?x_person="+  encodeURI(oj));
		}
		catch(ex) {
		var message = ex.getMessage();
	}

 

How to use JSON?

From above example. I used two JSON method which is JSON.parse and JSON.stringify. ‘response’ variable will contain the result in string format after executed.

JSON.parse = Convert string to object. As I need to add a message element  to contain message and assigning the status code to  the existing element.

JSON.stringify = Convert object to string. In order for me to be able to post back the values, I have to convert back to string before I postback.

Below are some helpful sites for your better understanding:

  1. JSON
  2. Mozzila Web Doc

 

Here is how to use it for posting back from client side to server side for processing in my post.

There you go. Hope you have a better understanding on this.

 

How about get some insight about window.onload() event

 

2 thoughts on “JSON Method

  1. Pingback: How to POST an array of objects with $.ajax – Web / Application Development Blog

  2. Pingback: How to POST an array of objects with $.ajax - Blogstxr

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s