1

I have been trying to make a call through jQuery to the Search REST API but I'm always getting' 401 Unauthorized, this is my code

var siteUrl = "http://myspserver:27180/sites/eyksmainportal";

    $(document).ready(function () {
        $("#queryDiv").append("ClientSiteType:ClientPortal* contentclass:STS_Site Reg=LA");

        $("#doSearch").click(function () {
            getSearchResultsREST($("#txtQuery").val());
        });
    });

    function getSearchResultsREST(queryText) {
        var searchURL = siteUrl + "/_api/search/query?queryText='" + queryText + "'";

        $.ajax({
            url: searchURL,
            method: "GET",
            headers: {
                "Accept": "application/json; odata=verbose"
            },
            contentType: "application/json; odata=verbose",
            success: SearchResultsOnSuccess,
            error: function (error) {
                $("#related-content-results").html(JSON.stringify(error));
            }
        });
    }

The response from Fiddler:

HTTP/1.1 401 Unauthorized
Content-Type: text/plain; charset=utf-8
Server: Microsoft-IIS/8.5
SPRequestGuid: b7908c9e-2c7b-c0f6-af64-6ebbf6790456 
request-id: b7908c9e-2c7b-c0f6-af64-6ebbf6790456
X-FRAME-OPTIONS: SAMEORIGIN
SPRequestDuration: 11
SPIisLatency: 34
WWW-Authenticate: NTLM
MicrosoftSharePointTeamServices: 15.0.0.4971
X-Content-Type-Options: nosniff
X-MS-InvokeApp: 1; RequireReadOnly
Access-Control-Allow-Origin: http://myspserver:36276
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: X-Requested-With, origin, content-type, accept,Content-Type,Accept,X-FORMS_BASED_AUTH_ACCEPTED,crossDomain,credentials
Date: Sat, 08 Sep 2018 01:49:27 GMT
Content-Length: 16
Proxy-Support: Session-Based-Authentication

401 UNAUTHORIZED

I have tried as well including the credentials on the header with PostMan and Fiddler but same behavior. Does anyone know what else should be configured or added on headers to get this working?

  • 1
    I was facing the similar issue few months back. If you are making the SharePoint calls from outside SharePoint please go through my answer. LINK Let me know if this resolves the issue or not. – Ankit Katiyar Sep 8 '18 at 6:15
  • Have you tried visiting the URI in the browser and not in Fiddler? Check if you have correct URI and see if its returning the data you wanted. – Billy Sep 8 '18 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.