我是Html编码的新手,我的目标是在单击按钮时发送测量值(在这种情况下为pH)。我不断收到错误消息,无法解析JSON字符串。我相当确定它与var ItemJSON及其表示方式有关。尝试过不同的方法,但希望有人可以指出正确的方向。预先感谢。
<script type="text/javascript" language="javascript">
function send()
{
var ItemJSON;
ItemJSON = '[ {"c8y_ph":{"ph":{"value":11,"unit":"na"}},"time":"2020-06-30T12:46:14.000+02:00","source":{"id":"681700"},"type":"c8y_ph"} ]';
URL = "https://myurl.com/measurement/measurements" ; //Your URL
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = callbackFunction(xmlhttp);
xmlhttp.open("POST", URL, false);
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.setRequestHeader('Authorization', 'Basic ' + window.btoa('username:pw')); //in prod, you should encrypt user name and password and provide encrypted keys here instead
xmlhttp.onreadystatechange = callbackFunction(xmlhttp);
xmlhttp.send(ItemJSON);
alert(xmlhttp.responseText);
document.getElementById("div").innerHTML = xmlhttp.statusText + ":" + xmlhttp.status + "<BR><textarea rows='100' cols='100'>" + xmlhttp.responseText + "</textarea>";
}
function callbackFunction(xmlhttp)
{
//alert(xmlhttp.responseXML);
}
</script>
<html>
<body id='bod'><button type="submit" onclick="javascript:send()">call</button>
<div id='div'>
</div></body>
</html>