【发布时间】:2017-04-07 13:34:08
【问题描述】:
当在 html 上填写表单时,我需要发送一个 json。我有以下html:
<head>
<title>Insert title here</title>
</head>
<body>
<form id="form">
Nome: <input type="text" id="nome" />
Idade: <input type="number" id="idade" />
<input type="button" id="submit" value="submit"/>
</form>
还有下面的js:
src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"
var arr = {
City : 'Moscow',
Age : 25
};
$(document).ready(function() {
//click on button submit
$("#submit").on('click', function() {
//send ajax
$.ajax({
url : 'http://localhost:8080/DBRest/rest/escreve',
type : "POST", // type of action POST || GET
dataType : 'json', // data type
contentType : 'application/json; charset=utf-8',
data : $("#form").serialize(), // post data || get data
data : JSON.stringify(arr)
})
});
});
我尝试创建一个 arr 变量来模拟填充的表单,但也没有得到任何东西。我想知道dataType和contentType的区别。关于这一行:
data : $("#form").serialize()
这是正确的吗? #form 周围的单引号和双引号之间存在区别
【问题讨论】:
-
您确定问题不是表单提交吗?也许尝试
$('#form').on('submit', function(e) { e.preventDefault(); //continue on with what you were doing });而不是监听提交按钮的点击。