我想POST/PUT以下文本
{ “电子邮件”: “[email protected]”}
via xhttp.send到外部服务器。我需要来发布双引号,但我该怎么做?我阅读了关于性能,特点等等,但我仍然在一个小时或更长时间后找不到解决方案。当然,这是行不通的(因为双引号的冲突):
xhttp.send("{"email":"[email protected]"}");
这是剧本到现在为止:
<!DOCTYPE html>
<html>
<body>
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("PUT", "http://www.example.com/json", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.setRequestHeader('X-CSRF-Token', "csrf_token");
xhttp.send("fname=Henry"); //TODO: MAKE SURE THAT EXACTLY {"email":"[email protected]"} WILL BE 'POSTED'.
}
</script>
</body>
</html>
请帮助我,我不知道这一点。
转义它们或使用单引号作为分隔符 –