I am currently getting this error 401 when I try to call my Sharepoint Endpoint. It occurred after I added the following Access-Control-Allow-Origin into the IIS header. I am unsure if this is an issue with IIS or the Sharepoint Configurations, please advice. Thank you!

My Response: GET http://www.test.com/Shared%20Documents/query%20result%20BEFORE.xml 401 (Unauthorized)

Previously, I was facing this error but has been resolved since I added the header:

Access to XMLHttpRequest at **** from origin '' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I added the Access-Control-Allow-Origin with a value of "*" in the HTTP Response Headers and now I am getting the 401 error.

My code:

  function testCall2() {
            const xhr1 = new XMLHttpRequest();
            const URL= "http://test.com/_api/search/query?querytext=%27BEFORE%27"

            xhr1.open("GET", URL);
            xhr1.onreadystatechange = function() {
                if (xhr1.readyState === 4) {
                    if (xhr1.status === 200) {
                        const response = xhr1.responseText;
                        // Process the response as needed
                        console.log(`XHR1 Success!! ${response}`);
                    } else {
                        console.error('XHR1 Error:', xhr1.statusText);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.