我正在尝试在不使用 AJAX 的情况下将数据发布到 REST api。我想以 JSON 格式发送数据。我有以下代码,但一直试图弄清楚如何转换输入字段并将其发布到服务器。这是我的代码尝试:
<form id = "myform" method = "post">
id: <input type = "text" id = "user_id" name = "user_id">
data: <input type = "text" id = "user_data" name = "user_data">
<input type = "button" id = "submit" value = "submit" onClick='submitform()'>
</form>
<script language ="javascript" type = "text/javascript" >
function submitform()
{
var url = '/users/' + $('#user_id').val();
$('#myform').attr('action', url);
//
// I think I can use JSON.stringify({"userdata":$('#user_data').val()})
// to get the data into JSON format but how do I post it using js?
//
$("#myform").submit();
}