0

我尝试“发布”数据,但是当我这样做时,我收到错误 400。我对“更新”数据有同样的问题。我不知道如何解决这个问题。

这是我的 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>';
}

这是我的控制器代码:

 @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);
    }
4

1 回答 1

0

我对 Java 不太熟悉,但我会运行像firebugfiddler web debugger这样的应用程序,并检查 ajax 请求是否正确构建(因为错误 400 与错误请求相关)

我过去已经建立了我的 JqGrid 添加/编辑/删除调用,就像我创建的这个非常老的 jsfiddle一样......但这确实对我有用。

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

..另外,我看起来不像你提供的控制器代码是网格的获取数据调用的代码......这与我猜的你的问题无关。(例如,您是否尝试向“RequestMethod.GET”操作执行发布)。您能否提供处理 POST 的方法的代码?

于 2013-01-08T08:28:51.547 回答