【问题标题】:controller get html date by ajax控制器通过ajax获取html日期
【发布时间】:2021-06-13 14:19:03
【问题描述】:

在我的 html 中,我需要输入一个日期值,然后,我将通过 ajax 将日期发送到与数据库连接的 MVC 控制器,以按日期获取记录。但是,我不知道如何将日期发送给我的控制器

getData() 中的 Ajax:

   $.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
        }

【问题讨论】:

    标签: java html ajax model-view-controller


    【解决方案1】:

    好吧,我在这里看到了很多问题。

    $.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){
    
    }
    

    【讨论】:

    • ajax发送数据的语法是什么
    • @Lucy 上面你使用的是=,而你应该使用:,如我的回答所示
    猜你喜欢
    • 2020-02-05
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多