I want to call a Salesforce REST URL from a Lightning Component. Specifically, I'm trying to call this Analytics API URL: /services/data/v32.0/analytics/reports
.
I see the following error in the JavaScript console:
Refused to connect to 'https://na15.salesforce.com/services/data/v32.0/analytics/reports' because it violates the following Content Security Policy directive: "connect-src 'self'".
I have a controller.js which calls to a helper.js which has the following in it in it. The sessionId is retrieved from an Apex controller that just returns UserInfo.getSessionId()
.
var action = component.get("c.getSessionId");
action.setCallback(this, function(a) {
var sessionId = a.getReturnValue();
console.log(sessionId);
$j.ajax("/services/data/v32.0/analytics/reports", {
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + sessionId);
},
success: function(response) {
alert('success!');
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.status + "," + textStatus + ": " + errorThrown);
}
});
});
$A.enqueueAction(action);
I'm positive that the REST request is valid as I'm able to make it successfully from a REST client (Postman).
I did see the Content Security Policy section in the Lightning Developer's Guide, but it doesn't seem to exclude Salesforce URLs:
The framework's CSP covers these resources:
JavaScript Libraries
All JavaScript libraries must be uploaded to Salesforce static resources. For more information, see Accessing JavaScript Libraries in Markup.
HTTPS Connections for Resources
All external fonts, images, frames, and CSS must use an HTTPS URL.
The security violation error message references a refusal to connect to na15.salesforce.com, even though I am just referring to a relative URL in the ajax call. Here is the csp report:
csp-report: {document-uri:https://na15.lightning.force.com/paura/widgets.app,…}
blocked-uri: "https://na15.salesforce.com"
document-uri: "https://na15.lightning.force.com/paura/widgets.app"
original-policy: "default-src 'self'; script-src 'self' https://ssl.gstatic.com/accessibility/ chrome-extension: 'unsafe-eval' 'unsafe-inline'; object-src 'self'; style-src 'self' chrome-extension: 'unsafe-inline'; img-src http: https: data:; media-src 'self'; frame-src https:; font-src https: data:; connect-src 'self'; report-uri /_/csp"
referrer: "https://na15.salesforce.com/_ui/common/apex/debug/ApexCSIPage"
status-code: 0
violated-directive: "connect-src 'self'"
Is it possible to access a Salesforce REST URL from a Lightning Component? If so, how and what am I missing?
Content-Security-Policy:default-src 'self'; script-src 'self' https://ssl.gstatic.com/accessibility/ chrome-extension: 'unsafe-eval' 'unsafe-inline'; object-src 'self'; style-src 'self' chrome-extension: 'unsafe-inline'; img-src http: https: data:; media-src 'self'; frame-src https:; font-src https: data:; connect-src 'self'; report-uri /_/csp