1

Im trying to write a script that will grab a series of attachments and zip them up using JSZip. All is working well but the script does a call to grab the download url

it seems like whats happening is this:

https://posse.my.salesforce.com/servlet/servlet.FileDownload?file=00PU0000009wleAMAQ

is redirecting to this

https://posse--devbox--c.cs9.content.force.com/servlet/servlet.FileDownload?file=00PU0000009wleAMAQ

and since posse--devbox--c.cs9.content.force.com and posse.my.salesforce.com are different domains i cant access the file because of CORS issues?

here is the full error

XMLHttpRequest cannot load https://posse--devbox--c.cs9.content.force.com/servlet/servlet.FileDownload?file=00PK0000001OAPR.

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://posse--devbox.cs9.my.salesforce.com' is therefore not allowed access.

Any ideas would be really helpful

1

You will have to channel through the VF proxy to resolve this error

https://gist.github.com/henriquez/3146782

https://gist.github.com/richardvanhook/4677449

The above gist should help and I have reproduced the content below so that helps even when link is broken .Replace with necessary URL and should help

<apex:page controller="CustomerCommunityController" id="customercommunitycontroller" sidebar="false" showHeader="false" standardStylesheets="false" >
 <head>
    <title>Acme Customer Support</title>
      <meta charset="utf-8" />     
     <apex:includeScript value="{!$Resource.jquery}"/>          
 </head>


 <script type="text/javascript">

 /* Same Origin Policy limits javascript running in VisualForce pages from making requests to 
    APIs, even salesforce apis, because they are hosted on salesforce.com while visualforce 
 pages are served from force.com.  To work around this, configure an ajax proxy in your organization.
 Ajax proxies are configured in setup under Security Controls | Remote Site Settings. Put in a URL    
 into the proxy like https://na1.salesforce.com (or whatever the domain of the api is)

 Once configured, just plain javascript can use the proxy url, which is available at /services/proxy
 The Salesforce Ajax Toolkit is not required, and generally you don't want to use that anyway, since
 its not supported on Safari or Chrome.

 Here's a basic example of how to make a request to the Chatter REST API from a Visualforce page.    
    This also works for portal(community) users and portal pages.
 */

  (function() {
    var $;

      $ = jQuery;

      $(function() {
       var credential = ' OAuth ' + '{!GETSESSIONID()}'; // native VF function
       var apiUrl = "https://na1.salesforce.com/services/data/v26.0/chatter/users/me";
    $.ajax({
        type: "GET",
        // for community pages, use whole community url including path, e.g. 
        // https://logan.blitz01.t.force.com/customers/services/proxy.
        url: "https://c.na1.visual.force.com/services/proxy", 
        contentType: 'application/json',
        cache: false,
        success : function(response) {
                      alert("result" + response);
               },
        error : function(response) {
                      alert("Failed" + response);
               },                 
        dataType: "json",
        beforeSend: function(xhr) {
            xhr.setRequestHeader('SalesforceProxy-Endpoint', apiUrl);
            xhr.setRequestHeader("Authorization", credential);
            xhr.setRequestHeader('X-User-Agent', 'MyClient');
        }


    });
  });

}).call(this);
</script>

 This is your new Page

</apex:page>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.