如何用速度模板语言调用ajax调用?

时间:2016-07-06 14:57:55

标签: javascript ajax velocity dotcms vtl

我正在使用力度模板语言和javascript工作联系我们。

要求是 a)客户从两个下拉列表(location和shopName)中选择值,然后单击Submit按钮

b)使用AJAX调用(无需重新加载页面)处理此过程,并在同一页面的下拉列表下方显示地址

我开发了表单的UI界面(在contact.vtl上)。在点击事件中我可以调用AJAX调用,在该控件转移到contact-detail.vtl后,在这里我无法继续进行?

如何将回复发送回contact.vtl?



function ajaxCall(){
        function getXMLRequestObject() {
            if(window.XMLHttpRequest) {
                return new XMLHttpRequest();
            } else if(window.ActiveXObject) {
                return new ActiveXObject("Microsoft.XMLHTTP");
            } else {
                alert("Your Browser does not support AJAX!! .Upgrade to latest version");
            }
        }

        var url="/application/vtl/widgets/contact-us/contact-detail.vtl?loc=XYZ&shop=ABC";

        var userContactReq = getXMLRequestObject();

        if ( userContactReq.readyState === 1 ||  userContactReq.readyState === 2 ||  userContactReq.readyState === 3 ) {
            userContactReq.abort();
        }
        userContactReq  = getXMLRequestObject();
        if ( userContactReq.readyState === 4 ||  userContactReq.readyState === 0 ) {
            userContactReq.open("GET",url,true);
            userContactReq.onreadystatechange = setReqSuccess;
            userContactReq.send(null);
        }

        function setReqSuccess() {

            if(userContactReq.readyState === 4){alert("Ready State is 4");}
            if(userContactReq.status === 200){alert("Status is 200");}

            if(userContactReq.readyState === 4 && userContactReq.status === 200) {
               
               ## How to recieve response here
                var responseString = userContactReq.responseText;
               

                console.log("end");
                alert(responseString);
            }
        }
    }




1 个答案:

答案 0 :(得分:0)

这与Velocity无关。这是一个javascript问题,无论你使用什么模板或标记,答案都是一样的。我认为你需要(通过javascript)用服务器的AJAX响应中的地址重写dom(设置div的内部HTML)以向用户显示它。