I am trying to call a REST web service from JavaScript using jQuery ajax and alert the response. Here is the HTML/JavaScript code
<html>
<head>
<script src="js/jquery.min.js"></script>
</head>
<body>
<script>
$.ajax({
type: "GET",
dataType: "jsonp",
url: "https://api.xyz.com/getItem/1013",
success: function(data){
alert(data);
}
});
</script>
</body>
</html>
I tested the API using CURL and browser, it is working fine. The response is as follows.
{
"endDate": "2017-04-27 10:03:17",
"startDate": "2017-03-28 10:03:17",
"status": "active"
}
When I am opening this page, I am getting the following error in the console.
SyntaxError: missing ; before statement
The browser points to the second line as error. I am not able to figure out if there is an issue with the API or client code. I tried in Firefox and Chrome browsers.
Note: I set the dataType
attribute in the ajax request as jsonp
, because I am using api.xyz.com
from app.xyz.com
which is a cross origin request. If I specify it as json
, I am getting another error.