Aug 21 2023 01:42 AM - edited Aug 21 2023 02:15 AM
I am currently trying to fetch my Sharepoint API via my code. However I am running into the issue below. I have already enabled the ISS Configurations as shown in the link below. However, the issue still persist.
Error:
index.html:1 Access to XMLHttpRequest at 'http://testing- sharepoint/Shared%20Documents/query%20result%20BEFORE.xml' from origin 'http://127.0.0.1:2233' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
My code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
function apiCallTest(searchValue) {
const adminAccount = 'testUser';
const adminAccountPassword = 'testPassword';
const auth = btoa(`${adminAccount}:${adminAccountPassword}`);
const testQuery = 'BEFORE';
const apiUrl= "http://testing-sharepoint/Shared%20Documents/query%20result%20BEFORE.xml"
const headers = {
'Authorization': `Basic ${auth}`,
// 'Accept': 'application/json;odata=nometadata',
// 'Content-Type':'application/json',
// 'Access-Control-Allow-Credentials':'TRUE',
// 'Access-Control-Allow-Headers':'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers,Authorization',
// 'Access-Control-Allow-Origin':'*',
// 'Access-Control-Allow-Methods':'GET,POST,PUT,OPTIONS'
};
$.ajax({
url: apiUrl,
headers: headers,
method: 'GET',
dataType: 'xml',
success: function(data) {
// Process the search results as needed
console.log('Search Results:', data)
},
error: function(xhr, textStatus, errorThrown) {
console.log("Cannot connect")
console.error('Error:', xhr, textStatus, errorThrown);
}
});
}
function checkConnection(url) {
if (navigator.onLine) {
console.log(`You are online. Checking connection to ${url}...`);
// You can attempt further actions here if needed
} else {
console.log('You are offline. Check your network connection.');
}
}
checkConnection("https://testing-sharepoint/_api/site");
apiCallTest();
});
</script>
</head>
<body style="background: black; color: aliceblue; font-size: 2rem;">
</body>
</html>