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
JeffTrullJeffTrull 

Using the REST API in Visualforce

I have previously made use of the AJAX Toolkit to create a generic VF component that displays selected fields of any sObject in a user-friendly grid, using the ExtJS library.  I was excited to give the new REST API a try, thinking that it would be both simpler and more performant than the SOAP-based Toolkit.

 

I've started coding up a new VF page using this approach, but have run into a fairly major stumbling block.  The XHR request is mysteriously failing; as far as I can tell it is because my Visualforce pages are of the form c.na7.visual.force.com/apex/whatever, while the REST API lives at na7.salesforce.com/services/data/v20.0, and this is considered to be a different "domain".

 

Have I correctly diagnosed the source of my problem?  If so, is there a chance for REST access in the future?

 

Thanks,

Jeff Trull

 

Best Answer chosen by Admin (Salesforce Developers) 
joshbirkjoshbirk

I believe you're correct, the JavaScript sandbox rules require an exact match, so even subdomains, etc., won't work.  There's a few things which might help enable this in the future.  For the moment, your options would mostly using a proxy of some kind. If I'm catching your name correctly,  I think we're getting you one the JavaScript Remoting trial, which isn't REST but can give you a similar framework/performance.

 

As an aside ... are you using Firebug or the like?  Does it show the security error on the transaction?

All Answers

joshbirkjoshbirk

I believe you're correct, the JavaScript sandbox rules require an exact match, so even subdomains, etc., won't work.  There's a few things which might help enable this in the future.  For the moment, your options would mostly using a proxy of some kind. If I'm catching your name correctly,  I think we're getting you one the JavaScript Remoting trial, which isn't REST but can give you a similar framework/performance.

 

As an aside ... are you using Firebug or the like?  Does it show the security error on the transaction?

This was selected as the best answer
JeffTrullJeffTrull

Thanks, Josh!  Yes, I'm interested in the Remoting trial.  The REST API was one of several ways I was experimenting with supplying data to ExtJS grid widgets.  I've also tried the AJAX Toolkit, "Visualforce Light" (having a separate page that only supplies data), actionFunction, and supplying data statically through <apex:repeat>.  The REST API would have the advantage of fitting very cleanly into the ExtJS framework, without much extra code.  But if it doesn't work, it's not a great loss - the other approaches are coded already.  I just wanted to do the comparison.

 

JeffTrullJeffTrull

And on your question about the transaction, I get either nothing (in which case I believe the Ext framework is trapping my request somewhere) or "302 Found", depending on the supplied URL, for example:

{!URLFOR('/services')}/data/v20.0/sobjects/Contact

 

forecast_is_cloudyforecast_is_cloudy

FYI - you might be interested in this post by Pat describing a workaround that lets you call the REST API from VF pages - http://blog.sforce.com/sforce/2011/03/calling-the-rest-api-from-javascript-in-visualforce-pages.html. Hope that helps.

Seema ASeema A

Hi ,

 

Even i am trying to do the same , in your case i think you were displaying the data , but i want to edit and save data of generic component . How to do that ?

 

Thanks

JeffTrullJeffTrull

For the record, Pat's workaround works perfectly... He describes it for jQuery but I did the analogous operation in ExtJS.  More info here.