I have developed a simple WebAPI 2 application which include this get method:
public HttpResponseMessage Get(int id)
{
var x = db.TESTS.ToList();
var formatter = new JsonMediaTypeFormatter();
return Request.CreateResponse(HttpStatusCode.OK, x, formatter);
}
I checked. It is returning Json value. I also checked it in Fiddler.
But when I want to retrieve this in a simple html file, I am getting parse error. My Web Service is published in localhost.
Web service url: http://192.168.5.154/mobileapi/api/values/2 is returning
[{"ID":2.0,"TEXT":"test 2"},{"ID":1.0,"TEXT":"test"}]
Here's the code.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Get Json value from WebAPI url</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<script type="text/javascript">
jQuery(document).ready(function ($) {
jQuery.ajax({
type: "GET",
url: "http://192.168.5.154/mobileapi/api/values/2?callback=?",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, status, jqXHR) {
alert("data is available");
},
error: function (jqXHR, status) {
alert(status);
}
});
});
</script>
</body>
</html>