Failed to load resource: the server responded with a status of 401 (Unauthorized)

7.3K    Asked by AndrewJenkins in Salesforce , Asked on Apr 23, 2021

I have Visualforce pages contains Lightning App, that contains Aura components hosted to Community aka Experience Builder, which are working fine when user is loggedIn, however, for guest users it is throwing "Failed to load resource: the server responded with a status of 401 (Unauthorized)"

for api.js and xdomain.js as shown in screenshot below.

enter image description here

Please find the below code for the VF page and controller class

 [removed] // Here 'VfApp' Is Lightning Application Name $Lightning.use("c:UserActionMenuApp", function () { $Lightning.createComponent("c:horizontalNav", { }, "horizontalNav", function (component) { console.log("component created"); }); }); [removed]  

Code for the Class file

public without sharing class CommunityUserDetailController { @AuraEnabled(cacheable=true) public static List getCurrentUser(){ System.debug('==getCurrentUser CALLED===>'); List users=new List(); if(UserInfo.getUserType()!='Guest'){ users= [SELECT Id, Name, profile.Name,SmallPhotourl,FullPhotoUrl FROM User WHERE Id =: Userinfo.getUserId()]; } System.debug('==getCurrentUser users===>'+ users); return users; } }

Any help on this will be appreciated

Thanks

Answered by Anisha Dalal

The 401 Unauthorized Error is an HTTP status code error that represented the request sent by the client to the server that lacks valid authentication credentials. It may occur client does not provide the proper authentication credentials to the server within the request time.


Methods to rectify the error”failed to load resource: the server responded with a status of 401 (unauthorized)”: The 401 Unauthorized error can be fixed by using any of the following ways: Check The URL: The 401 unauthorized error may occurdue to manual typing error. Hence, checking the URL and rectifying the mistakes in it will fix the 401 error status. Flush the DNS: Errors in DNS also give rise to 401 error status sometimes. Therefore, clearing the DNS will also rectify this error. In Windows, the DNS can be flushed by typing ipconfig/flushdns in the command prompt and clicking on ENTER. Clear Uncessasry Browser Cookie:Some times, browser cookies may not work smoothly leading to improper server authentication and leads to 401 error. Thus, the error can be rectified by clearing the cookies. Logging out and Logging in again: This error may also occur during the maintenance time of the websites. Therefore, visiting the website logging out and logging in again by providing the credentials may also rectify this 401error.



Your Answer

Interviews

Parent Categories