我能按照下面的方式发送请求吗?参数采用JSON格式对象进行分配。但我只得到了错误信息。但是当我使用REST客户端并选择RAW数据时,它可以正常运行。我猜想我一定是写错了代码。如何在JavaScript中发送原始JSON数据?有人能帮我吗?
xmlhttp = new XMLHttpRequest();
var url = "https://someURL";
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function () { //Call a function when the state changes.
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}
}
var parameters = {
"username": "myname",
"password": "mypass"
};
// Neither was accepted when I set with parameters="username=myname"+"&password=mypass" as the server may not accept that
xmlhttp.send(parameters);
JSON.stringify
,进行编码,然后发送请求。 - epascarello