0

I'm trying to post data to an external website from a wordpress site using Javascript, however when i send the form i'm getting this:

XMLHttpRequest cannot load https://external_site.com/embed/documents. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://my_site.com' is therefore not allowed access. The response had HTTP status code 404

I'm trying to this from a Wordpress site, this is the Javascript part that sends the data

jQuery('#send_doc').click(function(){
    var url = jQuery('#document_url').val();
    var name = jQuery('#name').val();

    var request = new XMLHttpRequest();

    request.open('POST', 'https://external_site.com/embed/documents');

    request.setRequestHeader('Content-Type', 'application/json');
    request.setRequestHeader('Authorization', 'Token my_token
    request.onreadystatechange = function () {
        if (this.readyState === 4) {
            console.log('Status:', this.status);
            console.log('Headers:', this.getAllResponseHeaders());
            console.log('Body:', this.responseText);
        }
    };

    var body = {
        'document_url': url,
        'name': name
    };

    request.send(JSON.stringify(body));
});

My question is, i understand that this is a Cross-domain problem, but my question is: Can i resolve the problem by my side or is this problem just on the externa site side?

Thank you very much for your answers

Sebastian Hernandez
  • 2,257
  • 4
  • 23
  • 32
  • Have you taken a look at CORS? http://stackoverflow.com/questions/5750696/how-to-get-a-cross-origin-resource-sharing-cors-post-request-working http://techblog.constantcontact.com/software-development/using-cors-for-cross-domain-ajax-requests/ – Ryan McDermott Oct 20 '15 at 21:07
  • Yes i've seen a lot of that but i haven't implemented any of the things those tutorials say 'cause i'm not sure if it should be implemented in the client side (My web page) or the server side (External API), do you have any hint? – Sebastian Hernandez Oct 20 '15 at 21:22
  • A few months ago I had to put together an API, both the client and server, and ran into CORS issues. I solved them on the external server with the proper headers. My research showed that recently browsers are enforcing the CORS requirements and there isn't anyway I found to avoid asking for it. I maybe wrong, but that is where I ended up at. – Practically Oct 21 '15 at 00:21
  • I believe both server and client configuration is required, at least for POST requests. – Ryan McDermott Oct 21 '15 at 02:27
  • It works well when using Post man plugin, so i think that there's no work to do in the server side. Could it be any WordPress restriction? – Sebastian Hernandez Oct 21 '15 at 14:35

0 Answers0