2012-09-18 33 views
0

你好我所有使用的是struts 2和jquery插件1.8。現在我搜索谷歌在struts中使用$.ajax()方法。但我thoing我沒有輸入正確的關鍵字。任何人都可以給我教程,我們如何使用struts來使用這個函數,並以String的形式處理響應。

+0

你問他們如何接口(Java-JS-Java)? – nmenego

回答

3

jQuery是一個JavaScript庫,它可以包含在支柱就像任何其他的框架,在JSP頁面中添加了jQuery

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.1.min.js" type="text/javascript" ></script> 

後,您可以使用它像

<script type="text/javascript"> 

$(function(){ //document ready handler to ensure that the jquery is executed after the DOM is loaded 

    //your code here 
$.ajax({ 
    url:'/your/path/', 
    type:'POST',//by default is GET 
    success:function(data){ 

    //success handler code 
    }, 
    error:function(jxhr){ 
    console.log("o0ps!!"); 
    } 

}); 
}); 

</script> 
+0

謝謝,但我將如何處理響應。什麼結果類型我不得不提到在'struts.xml' –

+0

可能是這個鏈接將幫助你http://stackoverflow.com/questions/8595080/how-do-i-access-the-request-in-struts-2我不熟悉java框架 – dakait

+0

不,你需要的是struts2-json-plugin在這裏使用這個插件解決了一些問題,包括struts.xml和annotations。此外,org.apache.struts網站上的插件文檔也有很好的幫助。 – Quaternion

0
$.ajax({ 
     type:'POST', 
     url:'ajaxAction?nodetitle='+title+'&filename='+fil, 
     dataType:'json', 
     success:function(data) 
     { 
      console.log(stringify(data));        
     } 
});  

說明

1.Type是方法類型:Get或Post。

2.url是你想在這裏重定向的地方ajaxAction它是一個你想要重定向的動作java文件。之後 ?節點標題和文件名是發送到操作頁面的兩個參數。

3.dataType是:您希望以何種格式返回數據。這裏以json格式。

4.成功功能:數據成功返回。那麼我們使用Stringify方法獲取數據,或者使用 服務器上的System.out.println。

相關問題