2015-10-13 69 views
0

更长的标题是:尝试使用Ajax调用到控制器上插入搜索结果到错误的表结果

“试图使用AJAX调用控制器插入搜索结果到一个表结果而留在同一个页面的结果为“405”或“直接自我参照导致循环......”错误”


我试图同时保持找到一种方法来填充搜索结果的表错误在同一页上使用ajax调用控制器。

ajaxCall->控制器 - >服务(完成搜索) - >控制器(来自搜索结果) - >回阿贾克斯与响应

我有被触发的形式提交防止违约后一个Ajax调用:

function ajaxGetSearchResults(link, form) { 
    var myList=[]; 
    var jqxhr = $.ajax({ 
     "url" : link, 
     "data" : form.serialize(), 
     "dataType" : 'json', 
     "type" : "POST", 
     "headers": { 
      'Content-Type': 'application/json' 
     }, 
     "success" : function (response){ 
      console.log("Ajax success"); 
      fillTable(response);  
      console.log("Search results added to table: "+response); 
     }, 
     "complete": function(response){ 
      console.log("Ajax call to controller completed"); 

     }, 
     "error": function(){ 
      console.log("Ajax call to controller triggered error"); 
     } 
    }); 
} 

在控制器我收到Ajax请求这样:

@RequestMapping(value = "/ajaxScriptSearch", method = RequestMethod.POST) 
public @ResponseBody List<ResultViewDto> processAJAXRequestSearch(@RequestParam String a1, 
     @RequestParam String a2, @RequestParam String a3, @RequestParam String a4) { 
     SearchDto searchDto = new SearchDto(); 
     searchDto.setAttribute1(a1); 
     searchDto.setAttribute2(a2); 
     searchDto.setAttribute3(a3); 
     searchDto.setAttribute4(a4); 
     try { 
      /* 
      calling Service, performing search using searchDto as a parameter, mapping result to resultViewDtos 

      */ 
     } catch(Exception e){ 
      /* do something */ 
     } 
     return resultViewDtos; 
} 

到服务的调用是全成。 resultViewDtos的一个例子是:[viewDto1,viewDto2,viewDto3]其中每个视图dto包含一些需要插入到表中的字符串值。

我似乎得到一个“HTTP状态405 - 请求方法'GET'不支持”的错误,但我的ajax调用是“type:POST”。

当我尝试使用GET insted时,我得到一个“直接自引用导致循环(通过引用链...)”的错误。

我使用杰克逊核心2.6.2,杰克逊数据绑定2.6.2,弹簧4,休眠4.


我会appericiate任何帮助我能......

+0

添加整个弹簧控制器代码 –

回答

0

在最后,我设法为此创建了解决方法。

我已经改变了我的Ajax调用这样:

function ajaxGetSearchResults(link, form) { 
    var jqxhr = $.ajax({ 
     "url" : link, 
     "data" : form, 
     "dataType" : 'json', 
     "headers": { 
      'Accept' : 'application/json', 
      'Content-Type': 'application/json' 
     }, 
     "type" : "GET", 
     "success" : function (response) { 
      console.log("Ajax success"); 
      fillTable(response); 
     }, 
     "complete": function(response) { 
      console.log("Ajax call to controller completed"); 

     }, 
     "error": function() { 
      console.log("Ajax call to controller triggered error"); 
     } 
    }); 
} 

而且我的控制器如下:

@RequestMapping(value = "/ajaxScriptSearch", method = RequestMethod.GET) 
public @ResponseBody List<String> processAJAXRequestSearch(@RequestParam String a1, 
    @RequestParam String a2, @RequestParam String a3, @RequestParam String a4) { 
    SearchDto searchDto = new SearchDto(); 
    searchDto.setAttribute1(a1); 
    searchDto.setAttribute2(a2); 
    searchDto.setAttribute3(a3); 
    searchDto.setAttribute4(a4); 
    List<String> result = new ArrayList<String>(); 
    List<ResultViewDto> resultViewDtos = new ArrayList<ResultViewDto>(); 
     try { 
      /* 
       calling Service, performing search using searchDto as a parameter, mapping result to resultViewDtos 
      */ 
      for(int i=0; i<resultViewDtos.size(); i++){ 
       result.add(resultViewDtos.get(i).toResponseString()); //a new method 
      } 
     } catch(Exception e){ 
      /* do something */ 
     } 
     return result; 
} 

toResponseString()是一种新的方法,我在resultViewDto创建返回一个字符串其中我需要的属性用“:”分隔。我填充结果并将其发送回ajax作为响应,然后将接收到的响应首先分割(','),以便将单个“行”等同于单个resultViewDto,然后按(': ')来获取每个单元格的值。

可能有更好的方法来解决它,但这就像一个魅力。 我希望这对其他人也有用。