2 qq 21392389 qq_21392389 于 2015.07.23 15:00 提问

Ajax点击按钮,没有反应

test.jsp

<%@ page contentType="text/html;charset=GB2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


AJAX提交请求测试页面








$(document).ready(function(){ $("#btn").click(function(){ $.ajax({ url: "AjaxEventHandleAction", type: "POST", data: {"AppCode":"50201A","DataSets":"2","Datas":"[{'name':'jim','age':'20'},{'name':'jim','age':'20'}]"}, dataType: "json", success:function(data) { var jsonStr = eval("("+data+")"); //alert(data); document.getElementById("info").innerHTML = "retAppCode: [ "+jsonStr.retAppCode+" ] DataSets: [ "+jsonStr.retDataSets+" ] 信息:[ "+jsonStr.retDatas[0].info+" ] "; //alert("success:OperCode="+jsonStr.OperCode); //alert("success:DataSets="+jsonStr.DataSets); //alert("success:Datas[0].name="+jsonStr.Datas[0].name); //alert("success:Datas[0].age="+jsonStr.Datas[0].age); }, error:function(data) { alert("error:"); } }); }); });

<s:submit value="%{getText('test')}" id="btn"/>


Web.xml

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

welcome.html
login.html
login.jsp

<listener>
    <listener-class>com.essa.config.InitSys</listener-class>
</listener>
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
     <error-code>404</error-code>
     <location>/error.jsp</location>
</error-page>
<error-page>
     <exception-type>java.lang.Exception</exception-type>
     <location>/error.jsp</location>
</error-page>
<error-page>
     <exception-type>401</exception-type>
     <location>/error.jsp</location>
</error-page>
<error-page>
     <exception-type>500</exception-type>
     <location>/error.jsp</location>
</error-page>
</web-app>

Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<package name="essa" extends="json-default">

    <interceptors>
        <interceptor name="essaBoundaryInterceptor" class="com.essa.Interceptor.essaBoundaryInterceptor">
        <param name="filterURISwitch">/welcome.html;/login;/html/help;</param> 
    </interceptor>

    <interceptor-stack name="essaDefaultStack">
        <!-- interceptor-ref name="json" /-->
        <interceptor-ref name="essaBoundaryInterceptor" />
        <interceptor-ref name="defaultStack" />
        </interceptor-stack>
    </interceptors>

    <default-interceptor-ref name="essaDefaultStack" />
    <default-action-ref name="DefaultAction" />
    <global-results>
          <result name="failPage" type="redirect">/login.jsp</result>
     </global-results>

    <action name="DefaultAction" class="com.essa.action.DefaultAction">
        <result name="LoginFail" type="redirect">/LoginFail.html</result>
    </action>

    <action name="AjaxEventHandleAction" class="com.essa.action.AjaxEventHandleAction">
        <result type="json"><param name="root">result</param></result>
    </action>

    <action name="LoginAction" class="com.essa.action.LoginAction">
        <result name="LoginOK" type="dispatcher">/website/home.jsp</result>
        <result name="LoginFail" type="redirect">/LoginFail.html</result>
        <result name="Normal" type="redirect">/welcome.html</result>
        <result name="Help" type="dispatcher">/html/help/help.html</result>         
    </action>

    <!-- 当调用具体的分值结转申请、异议申请等后台执行转长的生成文件,静态化等时,先生成等待进度条,配置一个createStaticsESLFileAction,配置<interceptor-ref name="execAndWait"拦截器>注释 -->    
</package>

<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.action.extension" value="action"/> 

Action:

package com.essa.action;

import java.io.IOException;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.essa.util.DealRequest;
import com.essa.util.VertifyRequest;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**

  • @name EssaAction
  • @time 2015-06-20
  • @version 1.0.0
  • @author ZhangDongxu
    */
    public class AjaxEventHandleAction extends ActionSupport {
    private static Logger logger = Logger.getLogger(AjaxEventHandleAction.class.getName());
    private static final long serialVersionUID = 1L;
    private String result;
    private String AppCode ;
    private String DataSets ;
    private String Datas ;
    private int res = 999;

    public String getResult() {
    return result.trim();
    }
    public void setResult(String result) {
    this.result = result.trim();
    }
    public String getAppCode() {
    return AppCode.trim();
    }
    public void setAppCode(String appCode) {
    AppCode = appCode.trim();
    }
    public String getDataSets() {
    return DataSets.trim();
    }
    public void setDataSets(String dataSets) {
    DataSets = dataSets.trim();
    }
    public String getDatas() {
    return Datas.trim();
    }
    public void setDatas(String datas) {
    Datas = datas.trim();
    }

    public String execute(){

    logger.info(">>>>>> [请求信息:AppCode = <"+AppCode+">,DataSets = <"+DataSets+">,Datas = <"+Datas+">]");
    try {
        res = VertifyRequest.vertifyRequest(AppCode,DataSets,Datas);
    } catch (IOException e) {}
    if(res!=0){
        logger.info(">>>>>> [请求校验失败:res = <"+res+">]");
        result = VertifyRequest.getErrorResult(AppCode,res,Datas);
    }
    else{
        logger.info(">>>>>> [请求校验成功:res = <"+res+">]");
        result = DealRequest.getResult(AppCode,DataSets,Datas);
    }
    result = JSONObject.fromObject(result).toString(); 
    return SUCCESS;
    

    }
    }
    /*

  • // ActionContext ac = ActionContext.getContext();
    // String rand = ac.getSession().size()+"";
    System.out.println(name+pwd);
    Datas = "[{'name':'"+name+"','pwd':'"+pwd+"'}]";
    System.out.println(Datas);
    logger.info(">>>>>> [锟斤拷锟斤拷锟斤拷息锟斤拷AppCode = <"+AppCode+">,DataSets = <"+DataSets+">,Datas = <"+Datas+">]");
    try {
    res = VertifyRequest.vertifyRequest(AppCode,DataSets,Datas);
    } catch (IOException e) {}
    if(res!=0){
    logger.info(">>>>>> [锟斤拷锟斤拷校锟斤拷失锟杰o拷res = <"+res+">]");
    result = VertifyRequest.getErrorResult(AppCode,res,Datas);
    }
    else{
    logger.info(">>>>>> [锟斤拷锟斤拷校锟斤拷晒锟斤拷锟絩es = <"+res+">]");
    result = DealRequest.getResult(AppCode,DataSets,Datas);
    }
    result = JSONObject.fromObject(result).toString();
    return "LoginOK";
    */

可以直接访问Action但是通过Ajax点击按钮访问的时候就没有访问

这个是拦截器:

package com.essa.Interceptor;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**

  • @name MyInterceptor
  • @time 2015-06-20
  • @version 1.0.0
  • @author ZhangDongxu
    */
    public class essaBoundaryInterceptor implements Interceptor {
    private static Logger logger = Logger.getLogger(essaBoundaryInterceptor.class.getName());

    /**

    • 自定义拦截器执行的代码
    • 作用:拦截请求
    • */ private static final long serialVersionUID = 1L;

    public void destroy() {}
    public void init() {}
    public String intercept(ActionInvocation invocation) throws Exception {
    logger.info(">>>>>> [请求信息:执行了自定义拦截器的代码!!!!]");
    return invocation.invoke();
    }

}

非常感谢

4个回答

sinat_29767257
sinat_29767257   2015.07.23 15:22

只想说看得很费劲,,

CSDNXIAOD
CSDNXIAOD   2015.07.23 15:04

IE9浏览器点击“添加到收藏夹”按钮没有反应问题的解决方法
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

yuke198907
yuke198907   2015.07.23 15:30

ajax不需要submit吧,建议看看ajax的写法

cuiwei1026522829
cuiwei1026522829   Ds   Rxr 2015.07.24 00:16

你不觉得你贴出来的代码特别乱么,不提交,看看是不是ajax请求没出发,还是参数写错了,,还是参数没传对

Csdn user default icon
上传中...
上传图片
插入图片