【问题标题】:Ajax post to spring mvc controller which get the data and set to datatablesAjax 发布到 spring mvc 控制器,获取数据并设置为数据表
【发布时间】:2020-11-14 03:38:51
【问题描述】:

我正在尝试使用 DataTable Ajax 请求向 Spring Boot 控制器发送带有一些数据的 POST 请求,并将数据设置到 Table 中。

HTML 表格:

<table id="assignmentDetails" class="display" style="width:100%">
                                    <thead>
                                        <tr>
                                            <th>Id</th>
                                            <th>title</th>
                                            <th>doc</th>
                                            <th>end</th>
                                            <th>start</th>
                                           
                                        </tr>
                                    </thead>
                                </table>

JQuery:

    $(document).ready(function(){
    $('#collapse'+id).on('shown.bs.collapse', function() {
        $('#assignmentDetails').DataTable({
            "ajax": {
                "url": "http://localhost:9091/assignment/getassignments",
                "dataSrc": '',
                "method": "POST",
                "timeout": 0,
                "headers": {
                  "Content-Type": "application/json"
                  },
                  "data":         JSON.stringify({"moduleId":"10010","subModuleId":null}),
                },
                "columns": [
                { data: "Id" },
                { data: "title" },
                { data: "doc" },
                { data: "start" },
                { data: "end" },
            ]
        } );
    });
            
});

在服务器上发送请求后,我收到Status Code 400 错误。 控制台错误:

2020-07-24 14:26:53.963 WARN 2724 --- [nio-9091-exec-7] .wsmsDefaultHandlerExceptionResolver:已解决 [org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:意外字符( '='(代码 61)):分隔根级值的预期空间;嵌套异常是 com.fasterxml.jackson.core.JsonParseException: Unexpected character ('=' (code 61)): 需要空格分隔根级值 在 [来源:(PushbackInputStream);行:1,列:3]]

谁能帮我使用datatable post reqeust向服务器发送POST请求

【问题讨论】:

    标签: jquery spring spring-boot datatable datatables


    【解决方案1】:

    如果它说的是意外字符,则您发送到控制器的 JSON 可能无效。在您的浏览器中,打开开发人员工具并尝试检查“网络”选项卡。通常,您可以找到正在发送的实际 JSON,并检查它是否有任何可疑之处。

    我还建议使用 jQuery post 函数,但这不是你的问题的一部分;)

    【讨论】:

    • 我在处理它时也注意到了这一点。你能告诉我如何使用数据表 Ajax POST 请求发送 POST 请求吗?我试试这个link 但它不起作用
    【解决方案2】:

    在花了 2 天的时间发送 POST 请求后 jQuery Ajax 调用带参数并获取响应。

    jQuery

    $("#demoButton").on('click', function(){
            var getData={};
            getData["mId"]=$("#mId").val();
            getData["sId"]=$("#sId").val();
        
             var tabla = $('#dataTable').DataTable( {
    
                ajax: {
                    url: getDataURL,
                    datatype : 'json',
                    data : function (  ) {
                        return JSON.stringify(getData);
                    },
                    "type": "POST",
                    "headers": {
                        "Content-Type": "application/json"
                      },
                      "dataSrc": function(data){
                          return data;
                      }
                    
                },
                "columns": [
                    { data: "Id"},
                    { data: "key2" },
                    { data: "key3" },
                    { data: "key4" },
                ]
            } );
        })
    }); // end of jQuery function
    <button id="demoButton">Demo Button</button>
        <table id="dataTable" class="display" style="width:100%">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>Value2</th>
                                                <th>Value3</th>
                                                <th>Value4</th>
                                            </tr>
                                        </thead>            
       </table>

    【讨论】:

      猜你喜欢
      • 2013-05-08
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 2019-03-28
      • 2015-01-31
      • 2014-01-22
      • 2016-05-06
      • 2013-01-11
      相关资源
      最近更新 更多