0

I am running a server on my localhost with an HTML5 application. I would like to send an http-request to a second server "130.100.100.100:50000".

I have the following JavaScript code running on localhost where I am sending http requests to the second server.

   var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {}
    };
        xmlhttp.open("GET", "http://130.100.100.100:50000/company.html/query?a0=" + a0, true);
        xmlhttp.setRequestHeader( 'Access-Control-Allow-Origin', '*');
        xmlhttp.send();

The second server is receiving my queries but I don't get any answer for my request. I think the problem is that the Same Origin Policy is violated. I tried to use the CORS standard to set

Access-Control-Allow-Origin: *

But the GET requests I am sending still doesn't contain "Access-Control-Allow-Origin" in the header. If I send the request manually with "Access-Control-Allow-Origin: *" everything is working proper. Do I have a mistake in my JavaScript code? I would like to avoid using jQuery.

Denis
  • 29
  • 2
  • 9

2 Answers2

0

You need to allow cross origin on server. Follow https://ma.ttias.be/set-access-control-allow-origin-cors-headers-apache-vhost-htaccess/

Kuldeep Singh
  • 696
  • 10
  • 25
0

You need a page used as a proxy. This page will send a GET request to the target page and display the response. Make sure that relative paths at img, a, script and link are converted to full paths. If you do this, the CORS problem will be fixed.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175