Ajax函数始终返回0并且不再继续

时间:2013-01-31 09:01:46

标签: java ajax jsp struts2

我有脚本代码:

<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接口。

2 个答案:

答案 0 :(得分:1)

尝试更改

中的javascript方法
xmlHttp.onreadystatechange=stateChangedmp();  

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

答案 1 :(得分:0)

class属性需要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和结果中的其他问题:&amp;