We're trying to call an API using JavaScript. There is an example JavaScript on the website of the API as below:
<!DOCTYPE html>
<html>
<head>
<title>JSSample</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(function() {
var params = {
// Request parameters
};
$.ajax({
url: "https://emi.azure-api.net/ICPConnectionData/single/?id={ICP}&" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","{subscription key}");
},
type: "GET",
// Request body
data: "{body}",
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
});
</script>
</body>
</html>
We have entered the {ICP} and {subscription key} but when we run it we get error every time.
The error we're getting is just the one from it failing and showing alert("error"). We tried using data.status and got error 0 "error".
Here's a screenshot of the console and the error in it.
New to using APIs like this and so assuming we're just missing something obvious?
Thanks in advance for your help.
The website with the API info is here
And here's the code I have so far so you can try it yourself:
<!DOCTYPE html>
<html>
<head>
<title>JSSample</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(function() {
var params = {
// Request parameters
};
$.ajax({
url: "https://emi.azure-api.net/ICPConnectionData/single/?id=0000212621UNA89&" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","f9d45d33880f4e0dac46255d9bc90fce");
},
type: "GET",
dataType: 'jsonp',
// Request body
data: "{body}",
success:function(data){alert(data)}
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
});
</script>
</body>
</html>