2009-06-26 96 views
0

我想通過來自JavaScript方法的ajax調用來調用Spring MVC控制器。javascript方法使用Prototype庫進行ajax調用。控制器將JSP作爲輸出。 我能夠打控制器,因爲我可以在日誌消息看不過的反應似乎得到lost.What可能是issue.Here是代碼....Spring MVC和Prototype JavaScript


    
function submitNewAjxCall() {
alert('test');
new Ajax.Request('SimpleApp/home.htm',
{
method:'post',
parameters: $('formId').serialize(true),
onComplete: showresult
});
}
function showresult(resultdata) {
alert(resultdata.responseText); ****//this method is not called.....****
}


home.htm點該控制器

public ModelAndView handleRequest(HttpServletRequest request, 
      HttpServletResponse response) throws Exception { 
     System.out.println("HomeController : " + ++i); 
     return new ModelAndView("home"); 
    } --- this throws home.jsp

感謝您的幫助。

+0

這是格式化的勝利: ) – skaffman 2009-09-10 08:51:44

回答

1

如果獲得Ajax響應並查看其內容是什麼,請選中Firebug(Net選項卡)。 也許有意義的是不返回整個HTML頁面,而是一個JavaScript特定的JSON對象,它告訴了一些關於控制器剛剛做了什麼的事情。也許添加一個ajax GET屬性到你的控制器,你只需輸出純JSON到Response Body而不是返回ModelAndView。嘗試在Prototype中使用onSucess。也許這會工作,然後

function submitNewAjxCall() 
{ 
new Ajax.Request('SimpleApp/home.htm?ajax=true', 
{ 
    method: 'post', 
    parameters: $('formId').serialize(true), 
    onComplete: function(transport) 
    { 
    alert(transport.responseText); 
    } 
}); 
} 

編輯:直接寫JSON(使用Flexjson作爲串行EG),你可以在你的(註釋)彈簧控制器使用:

@RequestMapping(value = "/dosomething.do", method = RequestMethod.GET, params = "ajax=true") 
public void getByName(
    @RequestParam(value = "name", required = true) String name, 
    HttpServletResponse response 
    ) 
{ 
    response.setContentType("application/json"); 
    try 
    { 
    OutputStreamWriter os = new OutputStreamWriter(response.getOutputStream()); 
    List<DomainObjects> result = this.domainObjectService.getByName(name); 
    String data = new JSONSerializer().serialize(result); 
    os.write(data); 
    os.flush(); 
    os.close(); 
    } catch (IOException e) 
    { 
    log.fatal(e); 
    } 
} 
+0

在Spring Controller中,我們需要將輸出直接寫回輸出流中作爲JSON.We將不得不擴展現有控件並提供此功能。 – Rajat 2009-09-09 18:32:39