jquery ajax send json to action and get response from action
来源:互联网 发布:数据共享怎么用 编辑:程序博客网 时间:2024/05/16 14:30
1.<script type="text/javascript" src="../../js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="../../js/jquery.json.js"></script>
<h1>jquery json action</h1>
<script type="text/javascript">
(function(){
var jsondata = {};
jsondata['tName'] = 'jacktian';
jsondata['tPassword'] = 'xyq123';
var data = {tUser: $.toJSON(jsondata)};
//alert(data.toString());
var sUrl = "${pageContext.request.contextPath}/ajax/JsonToActionAndBack_submit.action";
//var url = '<%=request.getContextPath() %>'+'/ajax/JsonToActionAndBack_submit.action';
//alert('sUrl:'+sUrl+' url:'+url);
$.ajax({ url : sUrl, dataType : 'json', type: 'POST', data : data, success : function(json){
if(json['status'] == 'S'){
alert('SUCCESS');
}else{
alert('SERVER ERROR!');
}
},error:function(){
alert("ERROR");
}});
})();
</script>
2.struts.xml
<package name="ajax" namespace="/ajax" extends="struts-default">
<action name="JsonToActionAndBack_*" class="com.tian.web.action.JsonToActionAndBack" method="{1}">
<result name="success">/pages/test/showBooks.jsp</result>
</action>
</package>
3.
package com.tian.web.action;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.Action;
public class JsonToActionAndBack implements Action,ServletRequestAware,ServletResponseAware{
class TUser{
private String tName;
private String tPassword;
public String gettName() {
return tName;
}
public void settName(String tName) {
this.tName = tName;
}
public String gettPassword() {
return tPassword;
}
public void settPassword(String tPassword) {
this.tPassword = tPassword;
}
}
private String tUser;
protected HttpServletRequest request;
protected HttpServletResponse response;
@Override
public String execute() throws Exception {
return null;
}
public String submit(){
System.out.println("json to action and back");
try {
response.getWriter().println("{/"status/" : /"S/"}");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String gettUser() {
return tUser;
}
public void settUser(String tUser) {
this.tUser = tUser;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.response = arg0;
}
}
- jquery ajax send json to action and get response from action
- Send and Receive JSON objects to Web Service Methods using jQuery AJAX in ASP.Net
- Posting array of JSON objects to MVC3 action method via jQuery ajax
- Magento: How to get controller, module, action and router name?
- Using the string repalce action in SharePoint workflow designer to send mail from mail template
- 关于 Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from 错误原因
- not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid
- Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=2269,
- struts2 配置 jquery ajax 调用 action 返回JSON 数据
- jquery ajax json struts2 action中的方法执行了两次
- jquery 给 action传递 json
- 整合struts2,jquery,json。采用jquery ajax,从struts2 action返回json类型数据
- 整合struts2,jquery,json。采用jquery ajax,从struts2 action返回json类型数据
- 整合struts2,jquery,json。采用jquery ajax,从struts2 action返回json类型数据
- 整合struts2,jquery,json。采用jquery ajax,从struts2 action返回json类型数据
- Json format to send notification from Parse
- Returning JSON from a ZF2 controller action
- 【Android】not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
- 浅谈流程变更最灵活的工作流软件
- WinCE开始菜单中添加应用程序
- vs2003 无法更改appconfig文件问题的解决
- 数据库水平切分的实现原理解析
- WinCE控制面板添加应用程序
- jquery ajax send json to action and get response from action
- 传输中指针的作用
- 在web开发中使用dynsrc被McAfee报木马病毒的解决
- .Net之随机时间生成器
- 设计模式汇总(转)
- python 中文问题
- 编写易于理解代码的六种方式
- 通信和通讯有什么区别?
- asp.net页面刷新