所以我有了这个HTML表单:如何使用HTML表单数据发送一个JSON对象
<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>
这将是当填写此表的数据作为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));
我在做什么错?
查看jQuery API中的'.ajax'和['serialize'](http://api.jquery.com/serialize/)。 –
它绝对必须是JSON对象吗?对象有什么结构? –
@AnthonyGrist是的,它必须是JSON,因为它是针对ReST服务的。 – kstratis