I'm trying to access an API using Fetch. I can't code from scratch so I've pieced this together various sources, including the API documentation source code:
<script type="text/javascript">
const url = 'https://apis.apprenticeships.sfa.bis.gov.uk/vacancies/v1/apprenticeships/search?standardLarsCodes=465';
var myHeaders = new Headers({
'Ocp-Apim-Subscription-Key':'xxxxxxxxxxxxxxxxxxxxxxxxx',
'Host':'apis.apprenticeships.sfa.bis.gov.uk',
'Access-Control-Allow-Origin':'xxxxxxxxxxxxxxxxxxxxxx'
});
fetch(url, {
mode: 'no-cors',
withCredentials:true,
headers: myHeaders
})
.then(function(response) {
data: "{body}"
})
.catch(function(error) {
console.log('Looks like there was a problem: \n', error);
});
</script>
Here's the example code provided by the API documentation (which uses Ajax in the HTML header):
$(function() {
var params = {
// Request parameters
};
$.ajax({
url: "https://apis.apprenticeships.sfa.bis.gov.uk/vacancies/v1/apprenticeships/{vacancyReference}?" + $.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");
});
});
When I run the code from my Wordpress site I get the following error:
Uncaught SyntaxError: Unexpected token '<'
When I run from my local test server I get:
Failed to load resource: the server responded with a status of 401 (Access Denied)
Any pointers on where I'm going wrong here?