[SalesForce] Using Canvas client-side proxy to access Apex REST API from Visualforce page

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.

Best Answer

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);
               }
    }
);
Related Topic