• denias
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

Hi to all!

I'm trying to run a simple visualforce page with JQuery Ajax request on it, to test how Apex Rest API is working. I am using current SessionId as a token, but I get a 401 Unauthorized Error. Can someone tell me what I am doing wrong?

 

Here is the code:

 

<apex:page >
<apex:includeScript value="{!$Resource.JQuery1_6}" />
<script type="text/javascript">
	var sessionId = 'OAuth {!$Api.Session_Id}';
	$(document).ready(function() {
		var url = "{!URLFOR('/services/data/v20.0/sobjects/Account')}";
		$.ajax({
			async: true,
			cache: false,			
            type: 'GET',
            url: url,       
            beforeSend: setHeader,
            success: function (result){
            	console.log(result);	
            }
        });
	});
    function setHeader(xhr) {
        xhr.setRequestHeader('Authorization', sessionId);
        xhr.setRequestHeader('X-PrettyPrint', '1');
   }	
</script>
</apex:page>

Thanks,

Denias


  • November 21, 2011
  • Like
  • 0