【问题标题】:Ajax function always return a 0 and does not proceed furtherAjax 函数总是返回 0 并且不再继续
【发布时间】:2013-01-31 09:01:46
【问题描述】:

我有脚本代码:

<script type="text/javascript" >

     var xmlHttp=null;
    function GetXmlHttpObject()
    {

        try
        {
            // Firefox, Opera 8.0+, Safari
            xmlHttp=new XMLHttpRequest();
        }
        catch (e)
        {
            //Internet Explorer
            try
            {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
    }
    function popSubCategory(){
        xmlHttp=GetXmlHttpObject()
        if (xmlHttp==null)
        {
            alert ("Browser does not support HTTP Request")
            return;
        }



        var url="<%= request.getContextPath()%>/populatePropertySubCategory.action";
          alert(url);


        xmlHttp.onreadystatechange=stateChangedmp();
        xmlHttp.open("POST",url,true);
        xmlHttp.send(null);
    }
    function stateChangedmp(){

        alert("Hello "+xmlHttp.readyState);
        if(xmlHttp.readyState==4)
        alert("lk"+xmlHttp.responseText);
    }
</script>

而我的Action 方法是

public String populatePropertySubCategory() {
        System.out.println("sub property called " );

        String errorXml = "This is a Sample to Check";

        response.setContentType("text/html");
        response.setHeader("Cache-Control", "no-cache");
        try {
            response.getWriter().write(errorXml);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return SUCCESS;

    }

我的动作支持xml是

 <action name="populatePropertySubCategory" method="populatePropertySubCategory" class="PropActionSupport">
            <interceptor-ref name="validation">
            <param name="excludeMethods">populatePropertySubCategory</param>
        </interceptor-ref>
        <result name="success" type="tiles" >
           submitProperty
            </result>
        </action>

现在当我调用此代码时,警报仅 0 并且不打印响应文本。

我已经实现了ServletResonseAware接口。

【问题讨论】:

    标签: java ajax jsp struts2


    【解决方案1】:

    只需尝试从

    更改 javascript 方法
    xmlHttp.onreadystatechange=stateChangedmp();  
    

    xmlHttp.onreadystatechange=function myFun {stateChangedmp();}
    

    【讨论】:

      【解决方案2】:

      class 属性需要类的完整限定名,包括包。

      而您只使用了一个Interceptor,而不是所有堆栈加上自定义的Validation 拦截器;

      将它们更改为:

      <action name="populatePropertySubCategory" 
              method="populatePropertySubCategory"
              class="org.foo.bar.PropActionSupport">
      
           <interceptor-ref name="defaultStack">
               <param name="validation.excludeMethods">
                  populatePropertySubCategory
               </param>
           </interceptor-ref>
      
           <result name="success" type="tiles" >submitProperty</result>
      </action>
      

      P.S:这是最明显的问题,但可能是 javascript 和结果图块部分中的其他问题:&

      【讨论】:

      • 一切都很好我只是显示了动作方法和方法被正确调用了方法调用没有问题
      • 你调用方法没有通过默认拦截器堆栈,行为是不可预测的,但不是你想要的肯定......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      相关资源
      最近更新 更多