[英]Form will not return required results
My form will not return the required results in JSON format, done by the script tag after the form.我的表单不会返回 JSON 格式的所需结果,由表单后的脚本标记完成。
Any advice as to what I am doing wrong?关于我做错了什么有什么建议吗?
<form action="/lookup/:url" method="GET">
<div class="inner-form">
<div class="input-field first-wrap">
<div class="svg-wrapper">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 "></path>
</svg>
</div>
<input id="search" type="text" placeholder="Paste a domain here" />
</div>
<div class="input-field second-wrap">
<button class="btn-search" type="submit">SEARCH</button>
</div>
</div>
<span class="info">ex. JeepBeef.com</span>
</form>
<script>
data = await fetch("/lookup/:url").then(result => result.json())
</script>
Listen for the form's "submit" Event , use Event.preventDefault() and than do your AJAX stuff.听取表单的“提交”事件,使用Event.preventDefault() ,而不是你的 AJAX 东西。
async function onFormSubmit(ev) {
ev.preventDefault();
const EL_form = ev.currentTarget;
return (await fetch(EL_form.action)).json();
}
const ELS_form = document.querySelectorAll("form[action]");
ELS_form.forEach((el) => el.addEventListener("submit", (ev) => {
onFormSubmit(ev).then(res => console.log(res));
}));
If later you decide to send your FormData with fetch()
如果稍后您决定使用
fetch()
发送 FormData
name="search"
for your ID search fieldname="search"
const FD = new FormData(EL_form);
fetch(EL_form.action, {method: 'post', body: FD})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.