• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
开水房
ASP.NET
博客园    首页    新随笔    联系   管理    订阅  订阅
struts2 提交 json

1. 提交简单的JSON数据

js

var params = {classCode:"fuck"};
            $.ajax({
                type: "POST",
                url: "http://localhost/public/Save.action",
                dataType: "json",
                data: params, // Post 方式,data参数不能为空"",如果不传参数,也要写成"{}",否则contentType将不能附加在Request Headers中。
                success: function(){alert(1)},
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown + ':' + textStatus); // 错误处理
                }
            });

 action:

View Code
String classCode;
public void setClassCode(String classCode) {
        this.classCode = classCode;
    }

2. 提交JSON数组

js:

var categoryJson = "{categoryJson:[ {'id' : 1, 'name' : '类型1', 'level' : 1}, {'id' : 2, 'name' : '类型2' , 'level' : 1} ]}";
            $.ajax({
                type: "POST",
                url: "http://localhost/public/Save.action",
                dataType: "json",
                data : "myJson=" + categoryJson,//
                success: function(){alert(1)},
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown + ':' + textStatus); // 错误处理
                }
            });

  action 要 implements ParameterAware

View Code
private Map parameters; //接收参数
     public void setParameters(Map parameters) {
        this.parameters = parameters;
      }
    public String save()
    {
         JSONObject json = new JSONObject();
          
            try {
              String[] params1 = (String[]) parameters.get("myJson");//接收myJson参数
                   String jsonStr = new String(params1[0]);
                   JSONObject jsonobj = new JSONObject(jsonStr);
                 
                   JSONArray jsonArray = jsonobj.getJSONArray("categoryJson");//json数组对应的键
                   int a = 1;
            } catch (Exception e) {

            } finally {

            }
        return SUCCESS;
    }

 

posted on 2013-02-17 13:53  白开水皮皮  阅读(2878)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3