Skip to main content

I've been looking all over the place to figure out how to approach this. I'm trying to call a web service (services/apexrest) from the same org via the client. I'm recieving the following error:

XMLHttpRequest cannot load https://cs45.salesforce.com/services/apexrest/RimsSalesforceConnectRemote. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://c.cs45.visual.force.com' is therefore not allowed access. The response had HTTP status code 401.

I noticed that the origin is different than the target.

  • Is there no way to call this web service via JavaScript?
  • Is this simply unsupported functionality?

I am able to make successful calls via

workbench.developerforce.com so I'm a bit confused.

What is also interesting is that the error eludes to a CORS issue, but the 401 code according to Salesforce has more to do with a bad token.

Instead of calling the Apex Rest web service via the $http Angular JS module, I decided to try a 2nd approach.  I leveraged the sforce.connect.remoteFunction nested inside of a promise to call the Apex rest web service. Unfortunately due to the requirements, I'm now bumping into a callout depth threshold:

[{"errorCode":"APEX_ERROR","message":"System.CalloutException: Callout loop not allowed\n\nClass.RimsSalesforceConnectRemote.sendRequest: line 19, column 1\nClass.RimsSalesforceConnectRemote.doPost: line 33, column 1"}]

To clarify my architecture is as follows: 

  • Client -> Apex Rest web service -> 3rd Party API

I'm utilizing this architecture as it allows me to securely store the 3rd party's static credenitals in the Apex web service.

So far I've tried to make direct calls to the Apex Rest web service via the $http AngularJS module and now via the sforce.connection.remoteFunction.

I definitely didn't think that simply calling the Apex Rest web service would be such a pain.  Testing via developer workbench was so simple. My hair is falling out.  

I'm looking for some advice in regards to architecture as well as what is the best way to call an Apex Rest web service (/services/apexrest/) via the client.
7 answers
  1. Jul 6, 2016, 1:35 AM
    Thanks for the response Chris.  What do you mean by "REST JSON directly to a seperate VF page"?
0/9000