6

Is it possible to use the client-side proxy provided by the Canvas SDK from a canvas app embedded within a Visualforce page to access an Apex REST endpoint?

We have a canvas app which calls an Apex REST endpoint

Sfdc.canvas.client.ajax('/services/apexrest/myapp/myservice', {
   client: Sfdc._sr.client,
   success: myCallback
});

This works correctly when the canvas app is loaded from the chatter tab; both the chatter page and the API are on the same domain, na7.salesforce.com, for example.

But when the canvas app is embedded in a Visualforce page, it doesn't work because the Visualforce page is on myapp.na7.visual.force.com, and the client-side proxy sends the request to na7.salesforce.com, causing a cross-origin error.

Any suggestions on making this work?

It would be great if the client-side proxy transparently made use of ajax proxy on the Visualforce domain.

2
  • In case this helps, I did get a VF page to invoke a Canvas App that was just a VF page in another SF Org: salesforce.stackexchange.com/questions/28524/… Apr 2, 2014 at 21:25
  • Thanks for the suggestion, @BritishBoyinDC. Your example works because it calls the Chatter API. The Chatter API and REST APIs can be accessed from Visualforce domains (see bit.ly/1hldPOg), but Apex REST URLs can't. You'll see that your request for the chatterFeedsUrl is sent to the visual.force.com domain rather than the salesforce.com domain.
    – xn.
    Apr 3, 2014 at 21:12

1 Answer 1

3

I have confirmed this is a problem. However there is a workaround

Sfdc.canvas.client.ajax('/services/proxy',
   {   
       client : sr.client,
       method: 'GET',
       contentType: "application/json",
       headers : {
           "SalesforceProxy-Endpoint" : sr.client.instanceUrl + "/services/apexrest/Account/001i000000iLoMf",
           "Authorization" : "OAuth "  + sr.client.oauthToken,
           "Accept" : "application/json"
       },
       success : function(data) {
                alert("Status: " + data.status);
               }
    }
);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .