如何在 Javascript 中发送 curl POST 请求

How to send a curl POST request in Javascript

如何在 javascript 中发送以下 curl POST 请求?有人可以帮我吗?

curl -X POST "https://IP" -H "accept: */*" -H "Content-Type: text/plain" -H "Authorization: Bearer XXXXX" -d "ON"

您可以使用此解决方案:

var xhr = new XMLHttpRequest();

xhr.open("POST", "https://IP", true);


xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("accept", "*/*");
xhr.setRequestHeader("Authorization", "Bearer XXXXX");

xhr.onreadystatechange = function() { // Call a function when the state changes.
    if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
        // Request finished. Do processing here.
    }
}

xhr.send(); or xhr.send(body);