1

I am trying to submit a HTML form from my VF page to different domain using jQuey syntax similar to below:

  $('#loginform').submit(function(){
            $.post($(this).attr('action'), $(this).serialize(), function(json) {
              alert(json);
            }, 'json');
            return false;
          });

This call is getiing blocked by browser with below error:

XMLHttpRequest cannot load https://domainurl.com/sessions/create3. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://c.ap2.visual.force.com' is therefore not allowed access.

How can I resolve above issue.

2

This is security feature which disable the call out from java script to any other domain. Here are details about it:

https://security.stackexchange.com/questions/8264/why-is-the-same-origin-policy-so-important

https://en.wikipedia.org/wiki/Same-origin_policy

Being said that there is workarounds for you.

Post your data back to VF page controller and then from your apex code make HTTP request

Here is example with explanation for your reference. http://www.saaspie.com/2014/06/05/cross-domain-ajax-requests-javascript/

The other option is using Ajax proxy.

sforce.connection.remoteFunction({
        url : "http://www.myExternalServer.com",
        onSuccess : function(response) {
               alert("result" + response);
           }
    });

https://developer.salesforce.com/index.php?title=Ajax_Proxy&oldid=31033

| improve this answer | |
  • Thank you for help. I can now understand what issue is all about. Somehow I can not move my code to apex controller. API call is expected to load some authentication cookies in browser which would not be possible by using a apex code. (apex server and my browser will be at different IP address and cookies are non-transferable) . Can you help me in how can I set 'Access-Control-Allow-Origin' header in destination domain. ? – Mukesh Verma Jun 2 '16 at 6:46
  • Did you check the last link? – AtulRajguru9 Jun 2 '16 at 7:26

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.