Jsforce: No Access-Control-Allow-Origin header is present message on bulk API response via Visualforce page

Created on 17 Dec 2014  路  6Comments  路  Source: jsforce/jsforce

I have a Visualforce page that I am using to bulk upload records from a csv file. In using the JSforce framework, I am creating my connection like this:

var conn = new jsforce.Connection({ 
                    accessToken: '{!$Api.Session_Id}',
                    serverUrl: 'https://cs7.salesforce.com',
                    proxyUrl: '/services/proxy'
                });

This org also has the My Domain option configured, and I have tried setting the serverUrl with the custom domain name as well.
I am able to create a job, and batches, and execute them, but when the "onresponse" event fires, I start getting this error message:

XMLHttpRequest cannot load https://mydomain--demo.cs7.my.salesforce.com/_ui/common/request/servlet/JsLoggingServlet. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://mydomain--demo--c.cs7.visual.force.com' is therefore not allowed access.

In my current codebase, I am creating the job manually:

var job = conn.bulk.createJob('SMBulk__c','insert');

And adding the batches to that job:

var batch = job.createBatch();
batch.execute(subfile);

I have also tried this using the bulk load method as well:

conn.bulk.load("SMBulk__c","insert",subfile,
                                     function(err,rets){});

Any help or advice would be greatly appreciated!!
Jim

question

Most helpful comment

I was wrong. It still needs proxyUrl when using bulk API, surely it accepts bulk API request but it redirects to other domain than Visualforce page. I think @jimrae 's last setting is the one how to connect to bulk API from Visualforce page.

conn = new jsforce.Connection({ 
    accessToken: '{!$Api.Session_Id}',
    proxyUrl: '/services/proxy'
});

All 6 comments

In Visualforce, you don't have to use proxyUrl or serverUrl in connection constructor - it is for web apps that resides out of salesforce.com domain. Bulk API endpoint is also in the same domain as your "My Domain" domain.

When I run my Visualforce page without the proxyUrl attribute, I get this error:

XMLHttpRequest cannot load https://mydomain--demo.cs7.my.salesforce.com/services/async/31.0/job. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://mydomain--demo--c.cs7.visual.force.com' is therefore not allowed access. The response had HTTP status code 400.

The bulk job does not process at all.

When I run it with the proxyUrl attribute, the bulk job processes, but I get the error I mentioned earlier:

XMLHttpRequest cannot load https://mydomain--demo.cs7.my.salesforce.com/_ui/common/request/servlet/JsLoggingServlet. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://mydomain--demo--c.cs7.visual.force.com' is therefore not allowed access.

I have tried various combinations of instanceURL and serverURL with no better results. My best result so far is with the proxyUrl, which allows the bulk job to process, but with the jslogginservlet error message.

There is no need to specify serverUrl and proxyUrl in Visualforce page. Please confirm that you are using the latest version (now 1.3.1).
The first one seems you are specifying serverUrl in connection option, and the second one is raised from other error than JSforce.

I downloaded the latest version of the code 1.3.1, but I still get the same

XMLHttpRequest cannot load https://mydomain--demo.cs7.my.salesforce.com/services/async/31.0/job. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://mydomain--demo--c.cs7.visual.force.com' is therefore not allowed access. The response had HTTP status code 400.

as before, when my connection looks like this:

conn = new jsforce.Connection({ 
                    accessToken: '{!$Api.Session_Id}'
                });

and no batch is processed. When I add the proxyURL like this:

conn = new jsforce.Connection({ 
                    accessToken: '{!$Api.Session_Id}'
                    ,proxyUrl: '/services/proxy'
                });

then the batch does get processed.

I was wrong. It still needs proxyUrl when using bulk API, surely it accepts bulk API request but it redirects to other domain than Visualforce page. I think @jimrae 's last setting is the one how to connect to bulk API from Visualforce page.

conn = new jsforce.Connection({ 
    accessToken: '{!$Api.Session_Id}',
    proxyUrl: '/services/proxy'
});

Thank you for the validation. That is the way I ended up setting up the
configuration for my project.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbonigomes picture jbonigomes  路  3Comments

fearphage picture fearphage  路  4Comments

sushicodeur picture sushicodeur  路  3Comments

shomanishikawa picture shomanishikawa  路  7Comments

JonDum picture JonDum  路  7Comments