0

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?

<FORM NAME="myform" ACTION="" METHOD="GET">Enter something in the box: <BR>
<INPUT TYPE="text" NAME="inputbox" VALUE=""><P>
<INPUT TYPE="button" NAME="button" Value="Click" onClick="loadurl(this.form)">
</FORM>

<script>
function getData(form) {
var baseUrl = "https://studentwebservices/rest/location/campus/";
var campus = form.inputbox.value;
var url = baseUrl + campus; 

// Code to be added


</script>

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.

Edited by Dani: Formatting fixed

3
Contributors
2
Replies
10
Views
5 Years
Discussion Span
Last Post by spatlola0422
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><br /> 
Last Name: <span id="lname"></span><br /> 
</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>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.