zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    1154 Vertex Coloring (25 分)
    7-4 Replacement Selection (30 分)
    7-3 Safari Park (25 分)
    7-2 The Judger (25 分)
    7-1 Prime Day (20 分)
    1101 Quick Sort (25 分)
    1093 Count PAT's (25 分)
    1128 N Queens Puzzle (20 分)
    N皇后问题
    1038 Recover the Smallest Number (30 分)
  • 原文地址:https://www.cnblogs.com/kevinge/p/2914220.html
Copyright © 2011-2022 走看看