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.

Recommended Answers

All 2 Replies

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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.