kst*_*tis 114 html javascript forms jquery json
所以我有这个HTML表单:
<html>
<head><title>test</title></head>
<body>
<form action="myurl" method="POST" name="myForm">
<p><label for="first_name">First Name:</label>
<input type="text" name="first_name" id="fname"></p>
<p><label for="last_name">Last Name:</label>
<input type="text" name="last_name" id="lname"></p>
<input value="Submit" type="submit" onclick="submitform()">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
当用户点击提交时,这是将此表单的数据作为JSON对象发送到我的服务器的最简单方法?
更新:我已经走了这么远,但它似乎不起作用:
<script type="text/javascript">
function submitform(){
alert("Sending Json");
var xhr = new XMLHttpRequest();
xhr.open(form.method, form.action, true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
var j = {
"first_name":"binchen",
"last_name":"heris",
};
xhr.send(JSON.stringify(j));
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Sac*_*tte 125
获取完整的表单数据作为数组和json字符串化它.
var formData = JSON.stringify($("#myForm").serializeArray());
Run Code Online (Sandbox Code Playgroud)
您可以稍后在ajax中使用它.或者如果你没有使用ajax; 把它放在隐藏的textarea中并传递给服务器.如果此数据通过普通表单数据作为json字符串传递,则必须使用json_decode对其进行解码 .然后,您将获得数组中的所有数据.
$.ajax({
type: "POST",
url: "serverUrl",
data: formData,
success: function(){},
dataType: "json",
contentType : "application/json"
});
Run Code Online (Sandbox Code Playgroud)
Que*_*tin 47
HTML无法从表单数据生成JSON.
如果你真的想从客户端处理它,那么你将不得不求助于使用JavaScript:
你可能最好不要坚持application/x-www-form-urlencoded
使用服务器而不是JSON处理数据和处理数据.您的表单没有任何可以从JSON数据结构中受益的复杂层次结构.
更新以回应重大改写的问题......
readystatechange
处理程序,因此你对响应没有任何作用Sor*_*ter 17
formData= new FormData(form)
JSON.stringify(Object.fromEntries(formData))
var form = document.getElementById('myForm');
form.onsubmit = function(event){
var xhr = new XMLHttpRequest();
var formData = new FormData(form);
//open the request
xhr.open('POST','http://localhost:7000/tests/v1.0/form')
xhr.setRequestHeader("Content-Type", "application/json");
//send the form data
xhr.send(JSON.stringify(Object.fromEntries(formData)));
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
form.reset(); //reset form after AJAX success or do something else
}
}
//Fail the onsubmit to avoid page refresh.
return false;
}
Run Code Online (Sandbox Code Playgroud)
摘自我在这里写的一篇文章:https ://metamug.com/article/html5/ajax-form-submit.html
您可以尝试以下操作:
<html>
<head>
<title>test</title>
</head>
<body>
<form id="formElem">
<input type="text" name="firstname" value="Karam">
<input type="text" name="lastname" value="Yousef">
<input type="submit">
</form>
<div id="decoded"></div>
<button id="encode">Encode</button>
<div id="encoded"></div>
</body>
<script>
encode.onclick = async (e) => {
let response = await fetch('http://localhost:8482/encode', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
})
let text = await response.text(); // read response body as text
data = JSON.parse(text);
document.querySelector("#encoded").innerHTML = text;
// document.querySelector("#encoded").innerHTML = `First name = ${data.firstname} <br/>
// Last name = ${data.lastname} <br/>
// Age = ${data.age}`
};
formElem.onsubmit = async (e) => {
e.preventDefault();
var form = document.querySelector("#formElem");
// var form = document.forms[0];
data = {
firstname : form.querySelector('input[name="firstname"]').value,
lastname : form.querySelector('input[name="lastname"]').value,
age : 5
}
let response = await fetch('http://localhost:8482/decode', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
let text = await response.text(); // read response body as text
document.querySelector("#decoded").innerHTML = text;
};
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
我迟到了,但我需要对那些需要对象的人说,只使用 html,有一种方法。在一些像 PHP 这样的服务器端框架中,你可以编写以下代码:
<form action="myurl" method="POST" name="myForm">
<p><label for="first_name">First Name:</label>
<input type="text" name="name[first]" id="fname"></p>
<p><label for="last_name">Last Name:</label>
<input type="text" name="name[last]" id="lname"></p>
<input value="Submit" type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
因此,我们需要设置输入的名称以object[property]
获取对象。在上面的例子中,我们得到了一个带有以下 JSON 的数据:
{
"name": {
"first": "some data",
"last": "some data"
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
364648 次 |
最近记录: |