Using a XMLHttpRequest with something like
xhttp.send('msg=message');
Causes a response from a server which returns "message" when asked to respond with req.body.msg. How do I approach the problem if I want to store the msg value to send in a variable and write the request to post the variable as msg? In other words how to let what comes after msg= be interpreted as variable and not string?
You could use template literals
to insert the variable value into the string
let text = "message";
xhttp.send(`msg=${text}`);
You could also just use +
let text = "message";
xhttp.send('msg=' + text);
Please show below code:
let text = "message";
var xhr = new XMLHttpRequest();
var params="msg="+${text};
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status==200) {
alert(xhr.responseText);
}
}
xhr.open('POST', url where you want post data, true);
xhr.send(params);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.