2016-03-21 51 views
0

也有類似的鏈接,但我還沒有找到任何解決方案爲我工作,所以我想知道如果有人可以給我一個工作示例我的方案。我正在做一個Ajax來從服務器端檢索數據,所以我可以在客戶端動態地創建圖表。我是否需要包含MappingJacksonHttpMessageConverter?如果這是答案,有人可以提供一個例子,我可以按照這個?Spring MVC發送responsebody對象到ajax後給出了406錯誤

的Java:

@RequestMapping(value="/getReportData.html", method=RequestMethod.GET, produces="application/json") 
public @ResponseBody Reports getReport1Data(HttpServletRequest request) 
{ 
    System.out.println("Report 1 Page GET Method"); 

    ModelAndView mv = new ModelAndView("report1"); 

    if((Reports)request.getSession().getAttribute(USER_SESSION_REPORTS) != null){ 
     reports = (Reports)request.getSession().getAttribute(USER_SESSION_REPORTS); 
     System.out.println("--------> Report 1 Page with session data"); 
     return reports; 
    } 
    else{ 
     System.out.println("--------> Report 1 Page with NO session data"); 
    } 
    mv.addObject("report1", reports.getReport1()); 

    return null; 
} 

的Javascript:

function getData(){ 
$.ajax({ 
    url: "getReportData.html", 
    type: "GET", 
    contentType: "application/json", 
    dataType: JSON, 
    success: function(report1){ 
     console.log("success: " + report1.utilRatio.decRatio); 
    }, 
    error: function(report1){ 
     console.log("error: " + report1.utilRatio.decRatio); 
    } 
}); 

}

響應頭: 內容語言: 「恩」, 內容長度:「1110 「 內容類型: 「text/html的;字符集= UTF-8」 服務器: 「Apache的狼/ 1.1」

請求報頭: 接受: 「/接受 - 語言: 「EN-US,連接; q = 0.5」 接受編碼: 「gzip的,放氣」 內容類型: 「應用/ JSON」 X-請求-隨着:「XMLHttpRequ est「

回答

1

看起來您的請求標題有誤。您可以刪除contentType設置,因爲您未將數據發送到服務器,並將dataType更改爲字符串值「json」而不是變量JSON。

此外,你的迴應標題是錯誤的。只要確保你總是返回一個Reports對象。你可能想從該端點刪除html擴展,因爲你只是返回一個對象。

+0

可惜我還在做後得到一個406錯誤以上: 1)僅保留dataType,並將其設爲「json」 2)始終在每個場景中返回Reports對象 3)刪除所有modelAndView對象 ....仍然得到406這是令人沮喪的,這是我需要了解/得到,我會做的唯一部分:( – Glen

+0

這些是我通過maven包括的圖書館,也許我失去了一個傑克遜我需要的圖書館? <! - JSON處理 - > \t \t \t com.fasterxml.jackson.core \t \t 傑克遜核 \t \t 2.5.1 \t \t \t \t 融爲一體。 fasterxml.jackson.core \t \t 傑克遜 - 數據綁定 \t \t 2.5.1 \t \t \t com.fasterxml.jackson.core \t \t 傑克遜 - 註解 \t \t 2.5.1 \t Glen

0

spring使用@ResponseBody annotaion將數據返回爲json .it會隱式地調用MappingJacksonHttpMessageConverter。因此您需要使用它。

@RequestMapping(value = "/getjson", method = RequestMethod.POST, produces = "application/json") 
@Transactional 
public void getJson(HttpServletRequest request, HttpServletResponse response, @RequestParam("type") String type) 
     throws DatatypeConfigurationException, IOException, JSONException { 
    JSONObject json = new JSONObject(); 
    Map<String, String[]> parameterMap = request.getParameterMap(); 
       List<Chart> chart=myService.getChart(); 
         if (Chart.size()>0) { 
       json.put("status", "SUCCESS"); 
       JSONArray array = new JSONArray(); 
       for (Chart chartData: chart) { 
        JSONObject object = new JSONObject(); 
        object.put("id", chartData.getRaasiId()); 
        object.put("name", chartData.getName()); 
        array.put(object); 
       } 
       json.put("options", array); 
      } 
     } 
    } 
    response.setContentType("application/json"); 
    System.out.println("response======" + json.toString()); 
    PrintWriter out = response.getWriter(); 
    out.write(json.toString()); 
} 

============ 上的HTML

   jQuery 
       .ajax({ 
        url : controllerUrl, 
        dataType : 'text', 
        processData : false, 
        contentType : false, 
        type : 'GET', 
        success : function(response) { 
    success : function(response) { 
         marker = JSON.stringify(response); 
         json = jQuery.parseJSON(marker); 
         json = JSON.parse(json); 
         alert(json.status); 
         } 
       }); 

供參考: https://rwehner.wordpress.com/2010/06/09/2-ways-to-create-json-response-for-ajax-request-in-spring3/

相關問題