-
-
Save ryanguest/5860817 to your computer and use it in GitHub Desktop.
This file contains 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 id="customercommunitycontroller" sidebar="false" showHeader="false" standardStylesheets="false" > | |
<head> | |
<title>Acme Customer Support</title> | |
<meta charset="utf-8" /> | |
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"/> | |
</head> | |
<script type="text/javascript"> | |
/* Same Origin Policy limits javascript running in VisualForce pages from making requests to | |
APIs, even salesforce apis, because they are hosted on salesforce.com while visualforce | |
pages are served from force.com. To work around this, configure an ajax proxy in your organization. | |
Ajax proxies are configured in setup under Security Controls | Remote Site Settings. Put in a URL | |
into the proxy like https://na1.salesforce.com (or whatever the domain of the api is) | |
Once configured, just plain javascript can use the proxy url, which is available at /services/proxy | |
The Salesforce Ajax Toolkit is not required, and generally you don't want to use that anyway, since | |
its not supported on Safari or Chrome. | |
Here's a basic example of how to make a request to the Chatter REST API from a Visualforce page. | |
This also works for portal(community) users and portal pages. | |
*/ | |
(function() { | |
var $; | |
$ = jQuery; | |
$(function() { | |
var credential = ' OAuth ' + '{!GETSESSIONID()}'; // native VF function | |
var apiUrl = "https://mysite.force.com/com1/services/data/v26.0/chatter/users/me"; | |
$.ajax({ | |
type: "GET", | |
// for community pages, use whole community url including path, e.g. | |
// https://logan.blitz01.t.force.com/customers/services/proxy. | |
//url: "https://c.na1.visual.force.com/services/proxy", | |
url: "https://mysite.force.com/com1/services/proxy", | |
contentType: 'application/json', | |
cache: false, | |
success : function(response) { | |
console.log("result" + response); | |
}, | |
error : function(response) { | |
console.log("Failed" + response); | |
}, | |
dataType: "json", | |
beforeSend: function(xhr) { | |
xhr.setRequestHeader('SalesforceProxy-Endpoint', apiUrl); | |
xhr.setRequestHeader("Authorization", credential); | |
xhr.setRequestHeader('X-User-Agent', 'MyClient'); | |
} | |
}); | |
}); | |
}).call(this); | |
</script> | |
This is your new Page | |
</apex:page> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment