標籤:
我是相當的不專業,寫給自己看。有什麼錯誤的話,說出來將感激不盡。
有兩種方法非同步傳輸
1.通過json字串: jsp中這麼寫:
$.getJSON( "details_ajaxAction",
function(data){
var json = eval("("+data+")");
alert(json.length);
for(var i=0;i<json.length;i++)
{
alert(json[i].xm);
}
}
struts中:<package name="ajax" extends="json-default">
<action
name="details_ajaxAction"
class="StudentAction"
method="queryDetails_ajaxInfo">
<result name="success" type="json">
<param name="root">result</param>
</result>
</package>
後台:把想要的資料對象放到List中,
Gosn gs= new Gosn();
result = gs.toJson(List).toString(); //result 就是傳到jsp的字串,result一定要有get‘set方法,與struts中的result一樣。
這個樣子就把result傳到了jsp回呼函數的data中去了。
2.另一種方法,傳四個List到jsp
jsp頁面中這麼寫:$.ajax({
url : "details_ajaxAction?xh=" + xh1,
type : ‘get‘,
dataType : ‘json‘,
error : function() {
alert("發生了一些程式猿都不知道的錯誤!");
},
success : function(responseResult) {
$.each(responseResult.stuList,function(i, item) {
});
$.each(responseResult.skList, function(i, item) {
});
$.each(responseResult.stList, function(i, item) {
});
$.each(responseResult.xqList, function(i, item) {
});
}
});
struts中: <package name="ajax" extends="json-default">
<action name="details_ajaxAction" class="StudentAction"
method="queryDetails_ajaxInfo">
<result name="success" type="json">
<param name="includeProperties">
stuList.*,skList.*,stList.*,xqList.*<!-- 這個要和JAVA類裡面的 參數一樣 並且要有set和get方法 -->
</param>
</result>
</action>
</package>
後台:定義四個 stuList,skList,stList,xqList,把想要的資料放進去就行了//要有getset方法。
這個樣子就把四個list中的資料傳到ajax回呼函數的responseResult中了,使用的時候就是responseResult.stuList.…….
就是這個樣子,沒有說原理,只說了做法。
關於ssh和ajax小小總結