控制器在 ajax 之前获得 html 日期
controller get html date by ajax
在我的 html 中,我需要输入一个日期值,之后,我将通过 ajax 将日期发送到我的 MVC 控制器,该控制器连接到数据库以按日期获取记录.但是,我不知道如何将日期发送到我的控制器
Ajax 在 getData() 中:
$.ajax({
url: 'send'
type: 'GET',
data: {date= $('#date').val,}
contentType: "application/json; charset=utf-8",
success: function(resp) {
//do something
},
error: function(err) {
//error
}
});
从用户处获取日期:
<input type="date" id="date" placeholder="date" onkeyup="getData()">
控制器:
@RequestMapping(value = "/send", method = RequestMethod.GET)
@ResponseBody
public List<Object> send(
@RequestParam(value = "date", required = false) Date date) throws Exception {
// do something
}
好的,我看到这里有很多问题。
$.ajax({
url: '//controller action url', // Missed the comma here
type: 'POST', // Changed to post
data: {"date": $('#date').val}, // Your syntax was wrong here
contentType: "application/json", // Simplified this. Not need but I just prefer it this way.
success: function(resp) {
//do something
},
error: function(err) {
//error
}
});
假设控制器动作类似于:
public ActionResult Send(DateTime date){
}
在我的 html 中,我需要输入一个日期值,之后,我将通过 ajax 将日期发送到我的 MVC 控制器,该控制器连接到数据库以按日期获取记录.但是,我不知道如何将日期发送到我的控制器
Ajax 在 getData() 中:
$.ajax({
url: 'send'
type: 'GET',
data: {date= $('#date').val,}
contentType: "application/json; charset=utf-8",
success: function(resp) {
//do something
},
error: function(err) {
//error
}
});
从用户处获取日期:
<input type="date" id="date" placeholder="date" onkeyup="getData()">
控制器:
@RequestMapping(value = "/send", method = RequestMethod.GET)
@ResponseBody
public List<Object> send(
@RequestParam(value = "date", required = false) Date date) throws Exception {
// do something
}
好的,我看到这里有很多问题。
$.ajax({
url: '//controller action url', // Missed the comma here
type: 'POST', // Changed to post
data: {"date": $('#date').val}, // Your syntax was wrong here
contentType: "application/json", // Simplified this. Not need but I just prefer it this way.
success: function(resp) {
//do something
},
error: function(err) {
//error
}
});
假设控制器动作类似于:
public ActionResult Send(DateTime date){
}