0

I want to send a variable/value that I have in client side javascript. The reason I am doing this is because my req.body.domain is coming back undefined, I am trying to send this to a nodejs route.

client side Java script

function digIt() {
    var xhr = new XMLHttpRequest();
    var domain = document.getElementById(digToolInput).value
    xhr.open('GET', "/tools", true);
    xhr.send(domain);

};
Malikiah
  • 149
  • 2
  • 2
  • 13
  • `GET` with a body doesn't really make sense. You should send it as a query string parameter or make a `POST`. See [HTTP GET with request body](https://stackoverflow.com/q/978061/691711). – zero298 Aug 03 '18 at 17:45
  • Hey there. Have a look at [how to ask](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve). Without more detail about both your requirements and your current state, I'm afraid there really isn't much we can do for you. – Fissure King Aug 03 '18 at 17:46
  • @zero298 I also tried this with a post request and that didn't work either, I thought I had it working at some point but maybe not. Which portion of that link am I supposed to read there is alot there I read a bit of it but it is just saying I cant use get but if not get then what should I be using? – Malikiah Aug 03 '18 at 17:53

2 Answers2

0

Note that value will go to the server by name variableNameInServer

var data = {
        'variableNameInServer': document.getElementById(digToolInput).value
    };
    $.ajax({
        type: 'POST',
        url: 'Your URL here',
        dataType: "json",
        data: data
    });
0

If you are using GET method, data should be as params or query.

xhr.open("GET", '/tools'+"?domain="+domain, true);

And on server receive req.params.domain.

Shahin Shemshian
  • 356
  • 1
  • 11