2013-01-08 189 views
0

我嘗試「發佈」數據,但是當我這樣做時,我收到錯誤400.我有與「更新」數據相同的問題。我不知道如何解決這個問題。錯誤400錯誤請求JqGrid

這是我的jqGrid代碼: $(函數(){

$.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); 
    } 
+0

我得到了與編輯項目時相同的錯誤。你能分享你如何解決你的問題嗎? –

回答

0

我不是太熟悉Java,但我運行一個應用程序,如firebugfiddler web debugger,並檢查ajax請求,看看它的構建是否正確(因爲錯誤400是錯誤的請求相關)

I'n the過去我已經建立了我創建的JqGrid添加/編輯/刪除電話really really old jsfiddle我創建了...但這對我來說確實有效。

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

..也,I'T看起來像您所提供的控制器代碼是get數據呼叫網格...這是不相關的問題,我猜碼。 (例如,您是否嘗試執行「RequestMethod.GET」操作的帖子)。你能否提供處理POST的方法的代碼?

+0

對不起。我插入了錯誤的方法 –