function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sony sonusony sonu 

rest call out using javascript not getting error 401 but executing in anonymous window

<apex:page>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="  crossorigin="anonymous"></script>
    
    <script>
    
        function Callout()
        {
            var JSONResponse = '';
            var JsonBody = '';
            JsonBody = '{"capital ": "delhi"}';
            
            
            
            
            console.log('--JsonBody ------->'+JsonBody );
            
            var xhr = new XMLHttpRequest();
            xhr.open("POST", "http://myendpointxxxxr", true);
            <!-- xhr.setRequestHeader("Authorization","Bearer {!AccessToken}" ); -->
            xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8");
            
            xhr.onload = function () 
            {
                JSONResponse = xhr.responseText;
                console.log('--JSONResponse ------->'+xhr.responseText);
            };
            xhr.send(JsonBody);
            setTimeout( function()
                {
                    if(xhr.status == 404){                     
                    }
                    else if(xhr.status == 401){
                    }
                    else if(xhr.status == 500){
                    }
                    else if(xhr.status == 200)
                    {
                        obj1 = JSON.parse(JSONResponse );
                        for(var x=0;x< obj1.length;x++)
                        {
                            var singleEle = obj1[x];
                            console.log('--singleEle ------->'+singleEle );
                        }
                    }
                },
            2000);
        }
        
    </script>   
    
    <apex:form>
        <apex:pageBlock>
             <apex:pageBlockButtons location="Top">
                <button type="button" onclick="Callout();" style="height:22px; width:150px" > 
     Make Callout 
                </button>
             </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
    
</apex:page>

iam getting the below errror :Failed to load resource: the server responded with a status of 404 (Not Found). any wrong in the code
pconpcon
This is because of CORS issues.  You will need to update your back end server to support cross origin requests.  This will vary from server to server, so you will need to research it based on how you are exposing the information.