954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to get response from a web service in JSON using JavaScript

Hi, I want to access a web service and get the response in json using javascript.
The web services can respond in json and gives me a table of records with a specific campus.
I input the campus code in the text box and create the url.
Now, I want to get the response in json and I'm really stuck.
Can anybody give me some clues please?

Enter something in the box:

From the information search process:
I know I have to change the HTTP Header accept and use application/json
and probably use JSONRequest.get()

This is my very first post on a forum.

evangelion89
Newbie Poster
2 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Here is sample json example, I hope you get something from it.

<html>
<body>
<h2>Create Object from JSON String</h3>
<p>
First Name: <span id="fname"></span> 
Last Name: <span id="lname"></span> 
</p> 
<script type="text/javascript">
var txt = '{"employees":[' +
'{"firstName":"John","lastName":"Doe" },' +
'{"firstName":"Anna","lastName":"adSmith" },' +
'{"firstName":"Peter","lastName":"Jones" }]}';

obj = JSON.parse(txt);

document.getElementById("fname").innerHTML=obj.employees[1].firstName 
document.getElementById("lname").innerHTML=obj.employees[1].lastName 
</script>
</body>
</html>
urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: