[英]How can I send the id of an element as a name using POST request?
我使用了application/x-www-form-urlencoded
因為對於application/x-www-form-urlencoded
,發送到服務器的 HTTP 消息的正文本質上是一個巨大的查詢字符串——名稱/值對由與號 (&) 和名稱通過等號 (=) 與值分開。 一個例子是: MyVariableOne=ValueOne&MyVariableTwo=ValueTwo
但我想將 id 作為變量發送,而不是作為值發送。 如何將 id 作為變量發送? 以下是我的程序: 提前致謝。
<HTML>
<head>
<script>
function fun1(element) {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/prc", true);
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhttp.send(element.id);
//alert("ID":element.id);
}
</script>
</head>
<body>
<button type="button" onclick="fun1(this)" id="img1">IMAGE</button>
</body>
</html>
您必須創建一個 FormData 對象,將您的數據添加到該對象並發送該對象。 請參閱此文檔。
var formData = new FormData();
formData.append("MyVariableOne", "ValueOne");
formData.append("id", 123456);
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/prc", true);
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhttp.send(formData);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.