m0_37812541
史莱姆Slime
2019-05-06 20:06

求大佬帮忙看看,在SSH框架中Action接受不到jquery_Ajax的data数据,该怎么办啊?

  • json
  • ajax
  • javascript
  • jquery
  • eclipse

我的jquery代码是这样的

<script type="text/javascript">
       $(document).ready(
          function(){
              $("#btnSearch").click(function(){
                 var id=$("#sid").val();
                  $.ajax({
                      type:"post",
                      url:"${pageContext.request.contextPath }/staffInfo_QuerySInfo.action",
                      contentType:"application/json;charset=UTF-8",
                      async:false,
                      data:{sid:id},
                      dataType:"json",
                      success:function(json){
                          console.log(json);
                          var i, x="";
                          x = json.dataMap.staffInfo.SId;
                          document.getElementById("content").innerHTML = x;
                      },
                      error:function(){
                          alert("error");
                      }
                  });
              }
              );
          }    
       );
   </script>

表单是这样的

<form class="navbar-form navbar-left" role="search" id="SearchById" >
                        <div class="form-group">
                              <div class="input-group">
                                    <input id="sid" name="sid" type="text" class="form-control" placeholder="输入编号查询"/>
                                    <span class="input-group-btn">
                                        <button id="btnSearch"  class="btn btn-default" >
                                            <span class="glyphicon glyphicon-search"></span>
                                        </button>
                                    </span>
                              </div> 
                        </div>
                    </form>

action的代码如下:

package sshpack.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import sshpack.entity.StaffInfo;
import sshpack.service.StaffInfoService;
public class StaffInfoAction extends ActionSupport {

    private StaffInfoService staffInfoService;

    public void setStaffInfoService(StaffInfoService staffInfoService) {
        this.staffInfoService = staffInfoService;
    }

    // 根据编号查询
    private Integer sid;

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    private Map<String, Object> dataMap;

    public Map<String, Object> getDataMap() {
        return dataMap;
    }

    public void setDataMap(Map<String, Object> dataMap) {
        this.dataMap = dataMap;
    }

    public String QuerySInfo() {

        String Sid = ServletActionContext.getRequest().getParameter("sid");
        if (Sid != null) {
            sid = Integer.valueOf(Sid);
        }

        StaffInfo staffInfo = staffInfoService.QueryStaffInfoById(sid);
        if (staffInfo != null) {

            dataMap = new HashMap<String, Object>();
            dataMap.put("staffInfo", staffInfo);
            dataMap.put("success", true);
            return "queryResult";
        } else {
            return "error";
        }
    }

Struts为:

<package name="json_data" namespace="/" extends="json-default">
        <action name="staffInfo_*" class="staffInfoAction" method="{1}">
            <result name="queryResult" type="json">
                <param name="root">
                                        dataMap
                                </param>
            </result>
        </action>
</package>

图片说明

我猜是不是这里这句话ServletActionContext.getRequest().getParameter("String")有问题啊,不用这个又要怎么才能接收数据啊

就像这样后台的action得不到数据,sid始终是空的,该怎么解决啊,找了好久都没找到解决办法........

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答