-1

I am not able to do a post request using XMLHttpRequest (The code will be run in client's side). Here's my code: (Basically I want to check the output of any python code by sending a post request to an online service that does this job for me)

var code = "print('hey')"
var input = "";

var data = new FormData();
var params = {'LanguageChoiceWrapper': "24",
        'EditorChoiceWrapper':'1',
        'LayoutChoiceWrapper':'1',
        'Program': code,
        'Input': input,
        'Privacy': '',
        'PrivacyUsers': '',
        'Title': '',
        'SavedOutput':'',
        'WholeError':'',
        'WholeWarning': '',
        'StatsToSave': '',
        'CodeGuid': '',
        'IsInEditMode': 'False',
        'IsLive': 'False'
        }

for (name in params) {
  data.append(name, params[name]);
}

var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://rextester.com/rundotnet/Run', true);
xhr.onload = function () {
    // do something to response
    console.log(this.responseText);
};
xhr.send(data);

When running it, this error happens enter image description here

When sending the same information through postman it works just fine and returns everything I want ... I ran into multiple threads but I was not able to make it work just by replacing the 'url' and 'params' fields in the answers of them. Here are two threads I ran through: 1 2

ArthurQ
  • 118
  • 1
  • 8
  • Access control headers are not set client side. They must be set by the remote server. If they aren't there and request is to a different origin then you will need to use a proxy on server you control or third party service – charlietfl May 01 '18 at 22:49
  • The console is stating a connection timeout and from trying to access the site myself: That seems to be the case unless the website isn't reachable over the internet. – pschichtel May 01 '18 at 22:51
  • Hi, is my answer working? – Jorge Fuentes González May 14 '18 at 09:54
  • @Jorge Fuentes Gonzáles +- :/ When I run the js code on my pc it makes the request if you check the "network" tab, but it is not returning any value :/ – ArthurQ May 14 '18 at 12:05
  • Read my last paragraph. That's the problem you are having. You cannot make request with different domain if the destination domain don't allows your domain to do so. Is a security measure impossible to overcome in default browsers. Search Google about `CORS`. – Jorge Fuentes González May 14 '18 at 12:15
  • Hi, did my answer clarified something for this problem? – Jorge Fuentes González May 23 '19 at 07:40

1 Answers1

0

You are connecting to a host with the HTTPS protocol, which connect to the port 443 by default.

As the error says you, the connection timed out. That means that the server is not accepting connections on that port.

I tried to telnet the domain rextester.com with the port 443 and returned connection error (The same as the browser) so the server don't has the port 443 open, which is the default for HTTPS requests:

enter image description here

Try making a HTTP request with this URL:

http://rextester.com/rundotnet/Run

HTTP has the default port 80, which the server has open actually:

enter image description here

Note that this will try to make the request to that domain, but if the destination domain don't allow cross-origin requests it will not work if the request is made from another domain. And this is unfixable as is impossible to avoid security measures (If with "client" you are talking about a browser).

Jorge Fuentes González
  • 10,682
  • 4
  • 38
  • 58