JqueryAjax如何传递JSON数据到controller,其中包括List数据? SpringMVC中使用List数据绑定,ajax怎么传输包...
\u4eceAPI\u63a5\u53e3\u83b7\u53d6\u7684json\u6570\u636e\u600e\u4e48\u5b58\u5230\u5bf9\u8c61\u7684List\u96c6\u5408\u4e2d1. \u7b80\u5355\u7684\u624b\u52a8\u653e\u7f6e \u952e\u503c\u5bf9 \u5230JSONObject\uff0c\u7136\u540e\u5728put\u5230JSONArray\u5bf9\u8c61\u91cc
List al = articleMng.find(f);
System.out.println(al.size());
HttpServletResponse hsr = ServletActionContext.getResponse();
if(null == al){
return ;
}
for(Article a : al){
System.out.println(a.getId()+a.getDescription()+a.getTitle());
}
JSONArray json = new JSONArray();
for(Article a : al){
JSONObject jo = new JSONObject();
jo.put("id", a.getId());
jo.put("title", a.getTitle());
jo.put("desc", a.getDescription());
json.put(jo);
}
try {
System.out.println(json.toString());
hsr.setCharacterEncoding("UTF-8");
hsr.getWriter().write(json.toString());
} catch (IOException e) {
e.printStackTrace();
}
\u590d\u5236\u4ee3\u7801
\u4e0a\u8ff0\u4ee3\u7801JSONArray\u662f\u5f15\u5165\u7684org.json.JSONArray\u5305
\u800c\u7528net.sf.json\u5305\u4e0bJSONArray\u7684\u9759\u6001\u65b9\u6cd5\uff1afromObject(list) \u8fd9\u662f\u7f51\u4e0a\u5927\u591a\u662f\u90fd\u662f\u76f4\u63a5\u7528\u6b64\u65b9\u6cd5\u5feb\u6377\u8f6c\u6362JSON\uff0c\u4f46\u662f\u5bf9\u4e8eHibernate\u7ea7\u8054\u64cd\u4f5c\u5173\u8054\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u4f1a\u62a5\u9519\uff0c\u5982\u679c\u5c06\u6620\u5c04\u6587\u4ef6\u4e2d\u7684\u7ea7\u8054\u914d\u7f6e\u53bb\u6389\u5c31\u884c\u4e86\u3002
\u53e6\u5916\u5bf9\u4e8elist\u7684\u8981\u6c42\u5c31\u662f\u5176\u4e2d\u7684\u5143\u7d20\u662f\u5b57\u7b26\u4e32\u6216\u5bf9\u8c61\uff0c\u5426\u5219JSON\u4e0d\u77e5\u9053\u4f60\u60f3\u8981\u7684\u662f\u4ec0\u4e48\u6570\u636e\u3002
<many-to-one name="cmsent" column="comment_tid" class="com.fcms.cms.entity.CmsComment"
not-null="false" cascade="delete">
\u4f46\u662f\u7ea7\u8054\u64cd\u4f5c\u6bd5\u7adf\u8fd8\u662f\u5f97\u5b58\u5728\uff0c\u5426\u5219\u4ee5\u540e\u6570\u636e\u5197\u4f59\u3001\u591a\u4f59\u3002
\u89e3\u51b3\u65b9\u6cd5\u5c31\u662f\uff1aJSONArray subMsgs = JSONArray.fromObject(object, config);
JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Object arg0, String arg1, Object arg2) {
if (arg1.equals("article") ||arg1.equals("fans")) {
return true;
} else {
return false;
}
}
});
\u7528spring mvc3\u7684\u6ce8\u89e3@ResponseBody
@ResponseBody
public List xxxx() {
...
List res = xxx
return res;
}
\u52a0\u4e24\u4e2a\u4f9d\u8d56
org.codehaus.jackson
jackson-mapper-asl
1.8.4
org.codehaus.jackson
jackson-core-asl
1.8.4
\u5982\u679c\u6ca1\u914d
\u8fd8\u9700\u914d\u7f6e
\u9644\u4e0a\u51fa\u5904\u94fe\u63a5\uff1ahttp://www.thinksaas.cn/ask/question/19691/
所以Ajax如何传递JSON数据到controller,其中包括List数据,其实只要序列化这个对象给ajax即可。
在jquery中用ajax传递接送数据的方法是:
以下代码示例:
$.ajax({
url: url,
type: "POST",
data: JSON.stringify(data), //注意这里一定要验证json格式
contentType: "application/json",
complete: callback
});
绛旓細鏂板缓涓や釜椤甸潰:1銆乻how.jsp锛氳皟鐢╝jax锛屽皢琛ㄥ崟涓殑鏁版嵁鍙戦佺粰ajax.jsp椤甸潰銆2銆乤jax.jsp锛氳幏鍙杝how.jsp椤甸潰浼犻鐨勮〃鍗曟暟鎹紝骞惰繑鍥炵粨鏋溿備袱涓〉闈㈢殑缂栫爜鏍煎紡瑕佽缃负GBK锛歴how.jsp椤甸潰鐨勯噸鐐归儴鍒嗭細1銆佹坊鍔犲 jquery-1.3.2.min.js 鐨勫紩鐢細2銆佽缃〃鍗曠殑id锛屽湪璋冪敤ajax鐨鏂规硶鏃惰鐢ㄥ埌銆3銆佽缃竴涓...
绛旓細鎵浠Ajax濡備綍浼犻扟SON鏁版嵁鍒癱ontroller锛屽叾涓寘鎷琇ist鏁版嵁锛屽叾瀹炲彧瑕佸簭鍒楀寲杩欎釜瀵硅薄缁檃jax鍗冲彲銆
绛旓細 (function(){ ("#txtEmail").bind("blur",function(){ var email = $.trim($("#txtEmail").val());// ajax楠岃瘉閭鈥斺斿紑濮 .ajax({url:"validateEmail.action?email="+email,type:"get",dateType:"json",success:function(obj){ if(obj.rely){ alert("閭鍙敤");}else{ alert...
绛旓細涓銆佺函鏂囨湰鏂瑰紡 1銆佸彂閫/鎺ユ敹鏁版嵁锛欳odeischeap.鐪嬩唬鐮侊細testJs.js //姝ゅ嚱鏁扮瓑浠蜂簬document.getElementById/document.all function$(s){if(document.getElementById){returneval('document.getElementById("'+s+'")');}else{returneval('document.all.'+s);}} //鍒涘缓XMLHttpRequest瀵硅薄锛屼互鍙戦...
绛旓細type: "POST"鍥犱负type涓簆ost~asp閲岄潰灏卞綋鍋氫竴鑸殑琛ㄥ崟鎻愪氦涓鏍峰鐞嗗氨濂戒簡銆傘浼犻鐨勪紶杈撶洿鎺ョ敤request()鎺ユ敹~鍦ㄤ綘杩欎釜妗堜緥閲岄潰~浣犲鐞嗗畬鐨勭粨鏋渵鏈鍚庤緭鍑000鍒002涔嬬被鐨勫氨濂戒簡銆傘
绛旓細褰撴湇鍔″櫒鏀跺埌Ajax璇锋眰骞跺鐞嗗悗锛屼細杩斿洖鏁版嵁缁欏鎴风銆俲Query鐨$.ajax鏂规硶鍙互澶勭悊杩欎簺杩斿洖鐨勬暟鎹紝骞舵牴鎹渶瑕佹墽琛岀浉搴旂殑鎿嶄綔銆備緥濡傦紝浣犲彲浠ラ氳繃鎴愬姛鍥炶皟鍑芥暟鏉ュ鐞嗘湇鍔″櫒杩斿洖鐨勬暟鎹紝鏇存柊椤甸潰鐨勬煇涓儴鍒嗐傛澶栵紝浣犺繕鍙互澶勭悊閿欒鎯呭喌锛屾瘮濡傜綉缁滈敊璇垨鏈嶅姟鍣ㄨ繑鍥炵殑閿欒淇℃伅銆傚洓銆jQuery Ajax鐨勪紭鍔 浣跨敤jQuery鐨...
绛旓細1銆侀鍏 锛屾秹鍙婁笟鍔¢昏緫鐨勮緭鍏ユ槸闇瑕侀氳繃鍙傛暟浼犻鐨勶紝涓昏鏈変笁绉嶆柟娉曪細path, query, POST/PUT body path: GET /api/user/123 鍏朵腑鐨123閫氳繃path浼犻 query: GET /api/search_user?userId=123 body: POST /api/user-signup {username: 'john'} 2銆佷笉寤鸿閫氳繃header浼犲弬鐨勫師鍥:1. proxy 鍜...
绛旓細jquery ajax閫氳繃header浼犻鍙傛暟鐨勫啓娉曞涓嬶細.ajax({ headers: { Accept: "text/plain; charset=utf-8", //鎸囧畾鎺ユ敹鐨勫叆鍙傜被鍨嬶紝鏅氭枃鏈 "Content-Type": "text/plain; charset=utf-8" //鎸囧畾濯掍綋绫诲瀷鍜岀紪鐮 } data: "data", //瀹為檯鍙傛暟 success : function(response) { // ...杩斿洖...
绛旓細缃戦〉鐨勮〃鍗曡緭鍏ユ鏄祻瑙堝櫒鐨刋MLHttpRequest鐨刪eader淇℃伅鍚戞湇鍔″櫒浼犻鏁版嵁鍙傛暟锛涜鐨刪eader鏄痟ttp鍗忚鐨刪eader澶撮儴鎵鎼哄甫鐨勬暟鎹紝涓嬮潰鏄jquery ajax鐨勪竴涓ず渚:.ajax({ type: 'POST',//璇锋眰鏂瑰紡get & post url: '璇锋眰鐨勫湴鍧',data: {'name':'鍙傛暟鍊'},success: function(data){ window.alert('杩斿洖...
绛旓細});//灞閮 绗竴绉 ('xxx').ajax({ //...beforeSend:function(jqXHR,options){ jqXHR.setRequestHeader("Content-Type", "application/json;charset=utf-8") ;jqXHR.setRequestHeader("Authorizationr", "Authorization") ;} //...}) ;//灞閮 绗簩绉 ('xxx').ajax({ //...headers:{ ...