2016-03-21 10 views
0

Benzer bağlantılar var ama benim için çalışmak için herhangi bir çözüm bulamadım, bu yüzden birisinin bana senaryo için bir örnek verebileceğini merak ediyordum. Sunucu tarafında veri almak için bir ajax alıyorum, böylece istemci tarafında dinamik olarak grafikler oluşturabilirim. MappingJacksonHttpMessageConverter'i eklemem gerekir mi? Eğer cevabınız varsa, birisi bunun için bir örnek verebilir mi?Bahar MVC ajax gönderiye yanıt vermek için 406 hatası veriyor

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

}

Yanıt Başlıkları: Content-Language: "tr", içerik-uzunluk: "1110 " Content-Type: "text/html; charset = utf-8" Sunucu: "Apache-Coyote/1.1"

talep Başlıkları: "/" yazısı kabul: Kabul Dil: "tr-tR, tr, q = 0.5" Accept-Encoding: "gzip, deflate" Content-Type: "application/json" X-Talep-With: "XMLHttpRequ est "

cevap

1

İstek başlıklarınız yanlış görünüyor. Verileri sunucuya göndermediğiniz ve contentType ayarını, JSON değişkeni yerine "json" dizesi değerine değiştirdiğinizden kaldırabilirsiniz.

Ayrıca, yanıt başlıklarınız yanlış. Her zaman bir Reports nesnesini döndürdüğünüzden emin olun. Ve muhtemelen bir nesne döndürdüğünüz için html uzantısını bu son noktadan kaldırmak istersiniz.

+0

maalesef hala yaptıktan sonra 406 hatası alıyorum yukarıdaki: 1) yalnızca veri tipini koru ve "json" yaptı 2) her senaryoda her zaman geri döndürülen Reports nesnesini 3) kaldırıldı tüm modelAndView nesneleri .... hala sinir bozucu olan 406 alıyorum, bu anlamaya/almam gereken tek parça budur: ( – Glen

+0

bunlar maven'e dahil ettiğim kütüphaneler, belki bir jackson eksik kütüphaneye ihtiyacım var? \t \t \t com.fasterxml.jackson.core \t \t Jackson çekirdekli \t \t 2.5.1 \t \t \t \t com. fasterxml.jackson.core \t \t Jackson-databind \t \t 2.5.1 \t \t \t com.fasterxml.jackson.core \t \t Jackson-açıklamalar \t \t 2.5.1 \t Glen

0

Yaylar, verileri geri döndürmek için @ResponseBody annotaion olarak kullanılır. Bu, MappingJacksonHttpMessageConverter öğesini dolaylı olarak çağırır. Referans için 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); 
         } 
       }); 

üzerinde

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

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