使用JQuery.js & JQuery.form.js 插件完成对StrutsAction的异步请求,返回JSON数据
来源:互联网 发布:怎么在ubuntu上安装qq 编辑:程序博客网 时间:2024/05/16 19:20
先说哈自己遇到的相当纠结的问题:
1. 本来使用jquery插件里面的$.getJSON 可以请求得到Action的数据,但是当传递参数给Action的时候,也得到了JSON的数据对象,但愣是不执行回调函数解析JSON数据,搞了一天也没搞好!
2. 后来使用$('#formID').submit(
function() {
$.ajax({
url: ........
.....
})
}
)遇到的是同样的问题,可以传进参数,可以得到Action返回的数据但愣是不执行jquery中的回调函数!
3. 最后改用jquery.form.js 插件测试程序,测试通过,下面将整合到自己的小项目里面!
步骤:
1. 引入jquery.js&jquery.form.js 插件,版本很重要,两个插件必须相配,具体使用FireBug调试
2. JQuery请求代码:(参考的是jquery form plugin 文档)
<script type="text/javascript">function loadInfo() { var options = { // target: '#info', // target element(s) to be updated with server response beforeSubmit: showRequest, // pre-submit callback success: showData, // post-submit callback // other available options: url: 'hello.action', // override for form's 'action' attribute //type: type // 'get' or 'post', override for form's 'method' attribute dataType: 'json' // 'xml', 'script', or 'json' (expected server response type) //clearForm: true // clear all form fields after successful submit //resetForm: true // reset the form after successful submit // $.ajax options can be used here too, for example: //timeout: 3000 }; // bind form using 'ajaxForm' $('#searchForm').ajaxForm(options); }// pre-submit callback function showRequest(formData, jqForm, options) { // formData is an array; here we use $.param to convert it to a string to display it // but the form plugin does this for you automatically when it submits the data var queryString = $.param(formData); // jqForm is a jQuery object encapsulating the form element. To access the // DOM element for the form do this: // var formElement = jqForm[0]; alert('About to submit: \n\n' + queryString); // here we could return false to prevent the form from being submitted; // returning anything other than false will allow the form submit to continue return true; } function showData(data) { $("#info").html("test");//清空info内容 $.each(data.comments, function(i, item) { $("#info").append( "<div>" + item.id + "</div>" + "<div>" + item.name + "</div>" + "<div>" + item.content + "</div><hr/>"); }); }// post-submit callback function showResponse(responseText, statusText, xhr, $form) { // for normal html responses, the first argument to the success callback // is the XMLHttpRequest object's responseText property // if the ajaxForm method was passed an Options Object with the dataType // property set to 'xml' then the first argument to the success callback // is the XMLHttpRequest object's responseXML property // if the ajaxForm method was passed an Options Object with the dataType // property set to 'json' then the first argument to the success callback // is the json data object returned by the server alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.'); } </script>
也贴下我的Action代码吧,测试用的!
package com.sure.action;import java.util.ArrayList;import java.util.List;import com.opensymphony.xwork2.ActionSupport;import com.sure.model.Comment;public class InfoAction extends ActionSupport {private List<Comment> comments = new ArrayList<Comment>();private String keywords;public String getKeywords() {return keywords;}public void setKeywords(String keywords) {this.keywords = keywords;}public List<Comment> getComments() {return comments;}public void setComments(List<Comment> comments) {this.comments = comments;}@Overridepublic String execute() throws Exception {System.out.println("keyword: "+this.keywords);loadComments();return SUCCESS;}/** * 加载留言信息 */private void loadComments() {Comment com = new Comment();com.setId(1);com.setName("木头人");com.setContent("123 木头人");comments.add(com);com.setId(2);com.setName("不动");com.setContent("123 不动不动");comments.add(com);}}4. struts.xml的配置文件的内容:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="json-default"> <action name="hello" class="com.sure.action.InfoAction"> <result type="json">index.jsp</result> </action> </package><!-- <include file="example.xml"/> --></struts>
- 使用JQuery.js & JQuery.form.js 插件完成对StrutsAction的异步请求,返回JSON数据
- 使用jquery插件jquery.form.js,异步提交表单 1
- jquery的ajax异步请求接收返回json数据实例
- Jquery form.js文件上传返回JSON数据,在IE下提示下载文件的解决办法,并对返回数据进行正确的解析
- jquery表单插件jquery.form.js的使用介绍
- 【js与jquery】jquery对返回json格式数据的处理和解析
- Jquery的表单插件 jquery.form.js
- 利用jquery插件jquery.form.js异步上传图片
- jquery异步请求返回JSON
- Jquery.form.js的使用
- jQuery Form表单插件jquery.form.js
- jQuery的Form插件jquery.form.js的简单入门
- 使用 jquery.form.js插件上传带有附件的表单
- struts2.5.1使用jQuery的ajax请求,返回json数据
- jq的json插件使用总结(jquery.json.min.js)
- jquery.form.js使用
- jQuery.form.js使用
- jQuery.form.js使用
- 数据结构之线性表1
- 数据库范式详解
- MyEclipse插件安装的几种方式
- 动态规划(3)Pots (BFS)
- 如何把数据库以二进制存储的excel,word文档写入excel,word
- 使用JQuery.js & JQuery.form.js 插件完成对StrutsAction的异步请求,返回JSON数据
- 1113 正/负数统计
- 开发者应该了解的 12 款 Eclipse 插件
- SQL Server默认值中的所有字段的列出
- Type Casting
- Java设计模式之十四(组合模式)
- 最长公共上升子序列(LCIS)的O(n^2)算法
- 1114 计算营业额
- Jquery优化效率 提升性能解决方案