2015-11-24 61 views
-1

我对这个错误有这些链接,但我的问题还没有解决。 Here,Here,我查了一些,但他们是基于json。所有Spring MVC Ajax 406不好的请求

首先我告诉你,同样的代码在我的另一个项目工作的事情是,我必须实现JSP和serlvets该项目,这一次在春季和休眠。

事情是请求来的方法。当它返回时。消息出现在控制台400错误请求中。

这是我的方法

@RequestMapping(value = "/populateBillItems") 
public @ResponseBody List<String> populateItems(@RequestParam String catName) { 
    itemNames = productService.getItemNames(catName); 
    return itemNames; 
} 

的Javascript

function populateItems(obj){ 
    var itemCategory = obj.value; 
    $.get('populateBillItems', { 
     catName : itemCategory 
    }, function(response) { 

     var select = $('#itemName'); 
     select.find('option').remove(); 
     $.each(response, function(index, value) { 
      $('<option>').val(value).text(value).appendTo(select); 
     }); 
    }); 
} 

在调试模式下,我得到的日食以下结果 Debug

你可以看到该值是从数据库中检索,但当响应返回时,这个错误来自控制台

GET http://localhost:8080/InventoryManagementSystemUsingSpring/populateBillItems?catName=Rice 406 (Not Acceptable) 

这是第一个问题。

我有jQuery中的另一个功能,发送请求到控制器,并得到ID和一个文本框填充它。它也给出了同样的错误上面一个是给..

function populateBillId(obj){ 
    var objValue=obj.value; 
    var table=""; 
    if(objValue=='Borrower'){ 
     table="BorrowerBill"; 
     alert(table); 
     $("#customerName").prop("readonly", true); 
    }else if(objValue=='Customer'){ 
     $("#customerName").prop("readonly",false); 
     table="CustomerBill"; 
    } 
    $.get('populateBillId', { 
     tableName : table 
    }, function(response) { 
     var id=response; 
     $("#billId").val(id); 
    }); 
} 


GET http://localhost:8080/InventoryManagementSystemUsingSpring/populateBillId?tableName=BorrowerBill 

页眉: enter image description here

的问题几乎解决了。现在,我得到我已经作出了效应初探方法的值这样

@RequestMapping(value = "/populateBillItems") 
public @ResponseBody String populateItems(@RequestParam String catName) { 
    itemNames = productService.getItemNames(catName); 
    String json=null; 
    json = new Gson().toJson(itemNames); 
    return json; 
} 

和modeified的JQuery这样

function populateItems(obj){ 
var itemCategory = obj.value; 
$.get('populateBillItems', { 
    catName : itemCategory 
}, function(response) { 

var select = $('#itemName'); 
select.find('option').remove(); 
    $.each(JSON.parse(response, function(index, value){ 
    $('<option>').val(value).text(value).appendTo(select); 
    })); 
}); 
}  

但现在的问题是,我也越来越(双引号)“”中的选项

这些都是与itemNames选择框和文本字段标识

enter image description here

报价
+0

400错误请求错误通常表示服务器在请求中收到的属性导致错误。我会检查你发送的内容以及服务器在做什么。你的JS代码不太可能成为问题(除了输入缺陷之外),我将开始调试'productService.getItemNames'调用。 –

+0

该请求正在服务器上,它甚至对其执行操作。但是在方法返回响应之后,这个消息就来了。 –

+0

您可以看到结果显示项目是从数据库中检索的。所以我认为它没有错误。 –

回答

0

我认为你的手术没有失败。

我看到你接受了HTTP 406消息。

你的后端服务是说,响应键入它返回在接受HTTP标头在您的客户端请求没有提供。

JSON的服务为application/json。您应该将其添加到您的标题。

w3.org - 10.4.7 - 406 Not Acceptable

编辑:

在你的方法,你给一个字符串返回到前端的时刻。尝试像这样转换json:

@RequestMapping(value = "/populateBillItems") 
public @ResponseBody String populateItems(@RequestParam String catName) { 
    itemNames = productService.getItemNames(catName); 
    String json = new Gson().toJson(itemNames); 
    System.out.println(json); // result before parsing 
    json = new JsonParser().parse(json).getAsString(); 
    System.out.println(json); // result after parsing 
    return json; 
} 
+0

我编辑了我的问题,问题几乎解决了。 –

+0

@MuhammadDanishKhan你可以在你的问题中显示你的双引号字符串/ json。 – Patrick

+0

好吧,我添加图片 –