Although this issue has been discussed numerous times before, I am struggling to identify the error in my code. The error message I am receiving is 'Uncaught SyntaxError: Unexpected token o'
Below is the ajax code I am using:
$.ajax({
type: "POST",
url: "json-http-server.aspx/GetDoctors",
data: '',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: myFunction,
failure: function (response) {
alert("AJAX error");
}
});
Here is the function responsible for parsing the returned data:
function myFunction(response) {
var arr = JSON && JSON.parse(response) || $.parseJSON(response);
var out = "";
out += "<table border='1'>";
out += "<tr><th>Title</th>";
out += "<th>Name</th>";
out += "<th>Gender</th>";
out += "<th>Address</th>";
out += "<th>Hospital</th></tr>";
for (var i = 0; i < arr.length; i++) {
out += "<tr>";
out += "<td>";
out += arr[i].Title;
out += "</td>";
out += "<td>";
out += arr[i].Name;
out += "</td>";
out += "<td>";
out += arr[i].Gender;
out += "</td>";
out += "<td>";
out += arr[i].Address;
out += "</td>";
out += "<td>";
out += arr[i].Hospital;
out += "</td>";
out += "</tr>";
}
out += "</table>";
document.getElementById("OutputDiv").innerHTML = out;
}
Below is the JSON data returned from a webservice:
'[{
"Title":"Univ. Prof. Dr.",
"Name":"John",
"Gender":"Doe",
"Address":"Washington DC, USA",
"Hospital":"Washington General Hospital"
},
{
"Title":"Univ. Prof. Dr.",
"Name":"Billy",
"Gender":"Joe",
"Address":"California, USA",
"Hospital":"AKH Univ-Kl.f.Innere Med. II"
},
{
"Title":"Univ. Prof. Dr.",
"Name":"Alex",
"Gender":"Haize",
"Address":"Michigan, 2500, USA",
"Hospital":"Rheuma-SKA Baden der SVA der gew. Wirtschaft"
}]'