SharePoint API Call function returns 401, but query works in browser.

Copper Contributor

Good Day,

I am currently facing issue regarding a SharePoint Error 401. The query is able to run in the browser however, when I try to call the URL in my code it does not work.

Technically if the browser is able to get the data my code should be able to fetch the query too right?


This is my code as follows, am I missing anything?

    function sharePointAPITestCall(){
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            headers: {"info1": sessionId},
            dataType: "json",
            success: async function (response) {
                const results = response.PrimaryQueryResult.RelevantResults.Table.Rows;

                for (const result of results) {
                    console.log(result.Cells)
                    const extension = result.Cells.find(cell => cell.Key === 'FileExtension').Value;
                    if (extension && extension.toLowerCase() !== 'aspx') {
                        const title = result.Cells.find(cell => cell.Key === 'Title').Value;
                        const url = result.Cells.find(cell => cell.Key === 'Path').Value;
                        let hitHighlightedSummary = result.Cells.find(cell => cell.Key === 'HitHighlightedSummary').Value;
                        console.log(hitHighlightedSummary)
                        try {
                            const xmlContent = await readUrlContents(url);
                            if (xmlContent) {
                                console.log(xmlContent)
                            }
                        } catch (error) {
                            console.error(error);
                        }
                    }
                }
            },
            error: function (xhr, status, error) {
                if (xhr.status === 500 || xhr.status === 400) {
                    alert('Data Fetch Failed. Connect with System Administrator.');
                } else {
                    alert('Data Fetch Failed. Try again sometime.');
                }
            }
        });
    }



0 Replies