Last active
August 29, 2015 14:16
-
-
Save karanrajs/d3cfc046f99c4b8f3008 to your computer and use it in GitHub Desktop.
Sample to code call REST API from visualforce page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page> | |
<apex:includeScript value="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"/> | |
<script> | |
jQuery(document).ready(function($) { | |
// Pull 10 Contacts via the REST API | |
$.ajax('/services/data/v28.0/query?q=SELECT+Name+FROM+Contact+LIMIT+10', | |
{ | |
beforeSend: function(xhr) { | |
// Set the OAuth header from the session ID | |
xhr.setRequestHeader('Authorization', 'Bearer {!$Api.Session_ID}'); | |
}, | |
success: function(response) { | |
$.each(response.records, function(index, record) { | |
//Add your logic | |
}); | |
}, | |
error: function(jqXHR, textStatus, errorThrown) { | |
// Display error message | |
alert(jqXHR.status + ': ' + errorThrown); | |
} | |
} | |
); | |
}); | |
</script> | |
<h1>Test REST API Without Proxy</h1> | |
</apex:page> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment