nginScript send POST resquest

matke nginx-forum at forum.nginx.org
Sat Feb 24 10:37:45 UTC 2018


Hi,
I tried to send some POST request to remote server (as part of external
authentication),  but I got message that XMLHttpRequest is undefined (or
similar error). 
I saw later in this post
https://forum.nginx.org/read.php?2,275459,275459#msg-275459 answer that this
is not possible.

Can it help if NodeJS is installed, or nginScript it completely independent?
(I don't have much experience in any javascript stuff)

This is how my script looks:


[code]
function baz(req, res) {
    res.headers.foo = 1234;
    res.status = 200;
    res.contentType = "text/plain; charset=utf-8";
    res.contentLength = 22;
    res.sendHeader();
    res.send("Enginx ");
    res.send("java");
    res.send("script\r\n");
    var response = "resp";


    var data = "{\n  \"page\": 0,\n  \"count\": 1,\n  \"order\": 2,\n 
\"sort\": \"0\",\n  \"headers\": {\n    \"Access-Control-Allow-Origin\": [\n
     \"*\"\n    ]\n  }\n}";

    var xhr = new XMLHttpRequest();
    xhr.withCredentials = true;

    xhr.addEventListener("readystatechange", function () {
      if (this.readyState === 4) {
    //    console.log(this.responseText);
      }
    });

    xhr.open("POST", "https://remote.host/api/login", false);

    xhr.setRequestHeader("origin", "https://localhost");
    xhr.setRequestHeader("content-type", "application/json");
    xhr.setRequestHeader("referer", "https://localhost/");
    xhr.setRequestHeader("accept-encoding", "gzip, deflate, br");
    xhr.setRequestHeader("accept-language",
"sr-RS,sr;q=0.9,en-US;q=0.8,en;q=0.7,hr;q=0.6,bs;q=0.5");
    xhr.setRequestHeader("cookie",
"securitytoken=eyJraWQiOiJ.......Swnq3xjEvXodQ");

    xhr.send(data);

    xhr.onreadystatechange = processRequest;

    function processRequest(e) {
        if (xhr.readyState == 4 && xhr.status == 200) {
            response = JSON.parse(xhr.responseText);
           // alert(response.ip);
        }
    }

    res.send(response);
    res.finish();
}
[/code]

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,278746,278746#msg-278746



More information about the nginx mailing list