简体   繁体   English

错误400错误的请求JqGrid

[英]Error 400 bad request JqGrid

I try to "POST" data, but when i'm doing it, i'm getting ERROR 400. I have the same issue with "UPDATE" data. 我尝试“发布”数据,但是当我这样做时,出现错误400。“更新”数据也有同样的问题。 I have no idea how to solve this problem. 我不知道如何解决这个问题。

This is my jqgrid code: $(function() { 这是我的jqgrid代码:$(function(){

    $.extend($.jgrid.defaults, {
        datatype: 'json',
        jsonReader : {
            repeatitems:false,
            total: function(result) {
                //Total number of pages
                return Math.ceil(result.total / result.max);
            },
            records: function(result) {
                //Total number of records
                return result.total;
            }
        },
        prmNames: {
            rows: 'max', 
            search: null
        },
        height: 'auto',
        viewrecords: true,
        rowList: [10, 20, 50, 100],
        altRows: true,
        loadError: function(xhr, status, error) {
            alert(error);
        }
    });

    $.extend($.jgrid.edit, {
        closeAfterEdit: true,
        closeAfterAdd: true,
        ajaxEditOptions: {
            contentType: "application/json"
        },
        mtype: 'PUT',
        serializeEditData: function(data) {
            delete data.oper;
            return JSON.stringify(data);
        }
    });
    $.extend($.jgrid.del, {
        mtype: 'DELETE',
        serializeDelData: function() {
            return "";
        }
    });

    var editOptions = {
        onclickSubmit: function(params, postdata) {
            params.url = URL + '/' + postdata.id;
        }
    };
    var addOptions = {
        mtype: "POST"
    };
    var delOptions = {
        onclickSubmit: function(params, postdata) {
            params.url = URL + '/' + postdata;
        }
    };

    var URL = 'rest/poll';
    var options = {
        url: URL,
        editurl: URL,
        colModel:[
        {
            name:'id', 
            label: 'ID',
            formatter:'integer',
            width: 40,
            editable: true,
            editoptions: {
                disabled: true, 
                size:5
            }
        },
        {
            name: 'categories.name', 
            index: 'categories', 
            label: 'Category',
            editable: true, 
            edittype: "select",
            viewrecords:true,

            editoptions: {
                dataUrl:"rest/category/list.do", 
                buildSelect: function (list) 
                { 
                    var data = jQuery.parseJSON(list); 
                    var s = '<select>'; 
                    for (var i = 0; i < data.locationList.length; i++) {
                        s += '<option value="' + data.locationList[i] + '">' + data.locationList[i].name + '</option>'; 
                    } 
                    return s + "</select>"; 
                } 
            } 
        },
        {
            name:'name',
            label: 'Name',
            width: 300,
            editable: true,
            editrules: {
                required: true
            }
        },

        {
            name: 'MyLink',
            formatter: myLinkFormatter
        }

        ],
        caption: "Polls",
        pager : '#pager',
        height: 'auto',
        ondblClickRow: function(id) {
            jQuery(this).jqGrid('editGridRow', id, editOptions);
        }
    };

    $("#grid")
    .jqGrid(options)
    .navGrid('#pager',
    {}, //options
        editOptions,
        addOptions,
        delOptions,
        {} // search options
        );

});

function myLinkFormatter (cellvalue, options, rowObjcet) {
    return '<a href = "/Polls_V2/options?categoryId=' + options.rowId + '">Answers</a>';
}

And this my controller code : 这是我的控制器代码:

 @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<String> createCategory(HttpServletRequest request, @RequestBody Polls polls) {
        pollsService.add(polls);

        URI uri = new UriTemplate("{requestUrl}/{username}").expand(request.getRequestURL().toString(), polls.getId());
        final HttpHeaders headers = new HttpHeaders();
        headers.put("Location", Collections.singletonList(uri.toASCIIString()));
        return new ResponseEntity<String>(headers, HttpStatus.CREATED);
    }

I'm not too familiar with Java, but I'd run an app like firebug or fiddler web debugger , and inspect the ajax request too see if its building up correctly (as error 400 is bad request related) 我对Java不太熟悉,但是我运行了诸如FirebugFiddler Web调试器之类的应用程序,还检查了ajax请求,以查看其是否正确建立(因为错误400与错误的请求相关)

I'n the past I've build up my JqGrid add/edit/delete calls like this really really old jsfiddle I created... but that did work for me. 我过去已经建立了我的JqGrid add / edit / delete调用,就像我创建的这个非常老的jsfiddle一样,但这确实对我有用

DataGrid.navGrid('#pager', { edit: true, add: true, del: true, search: true },
           {                   
                url: this.urlEdit, ...
            },
            {                   
                url: this.urlAdd,  ...
            },
            {                   
                url: this.urlDelete, ...
            }
        );

.. also, i't looks like the controller code you supplied is the code for the get data call for the grid... Which isn't relevant to your question I'm guessing. ..同样,我看起来您提供的控制器代码不是用于网格的get data调用的代码...这与您正在猜测的问题无关。 (eg are you trying to perform a post to a "RequestMethod.GET" action). (例如,您是否尝试执行“ RequestMethod.GET”操作的发布)。 Can you please supply the code for the method that handles the POST? 您能否提供处理POST方法的代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM