[英]Form data in self.body is null. I am using Preact and Total js. value="@{M.email}" is not working. It populates the form with this value
submitHandler 是我調用 onSubmit 並使用 XMLHttpRequest 發送數據的函數。 我正在使用 xhr.Send() 發送數據,但在控制器中,在 self.body 中我得到空值。
class Form extends Component {
render(props, state) {
<div>
<div class="field">
<label class="label">PHONE NUMBER</label>
<div class="control">
<input
class="input"
type="tel"
placeholder="+91 "
name="phone"
value="@{M.phone}"
onInput={linkstate(this, "phone")}
/>
</div>
</div>
</div>;
}
}
export default Form;
submitHandler = () => {
let formData = new FormData(document.getElementById("signup"));
let xhr = new XMLHttpRequest();
xhr.open("POST", "/xhr", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
console.log("Request finished");
}
};
xhr.onload = () => {
alert(xhr.responseText);
};
xhr.send(formData);
};
您確定formData
包含一些數據嗎? 檢查 Web 開發人員工具中的請求並嘗試捕獲數據。
根據我的理解 Self.body 需要一個 JSON 對象。 我將 formData 轉換為 JSON 對象並且它可以工作。 xhr.send(JSON.stringify(Object.fromEntries(formData)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.