将参数从AJAX传递到JSP页面
我试图将参数从AJAX传递回我的JSP页面。这是我的示例代码:
$(document).ready(function() {             $.ajax({
            type: "GET",
            url: "URL...",
            dataType: "xml",
            success: function(xml) {
                $(xml).find('Rowsets').each(function(){ 
                            var x = $(this).find('Auto_Id').text() // Assign data from Auto_Id into variable x
                    document.form.y.value = x; // Pass the parameter back to the JSP page
                    });
                }
    });
});
<FORM name="form"><input name="y" value="" /></FORM> //value left blank for AJAX to auto-populate上面的代码有效-我能够获得参数x。但是,是否可以在同一.JSP页面上将x的值转换为以下格式?
<%= session.getAttribute("x") %>或者,获取x的值并将其传递给Java标签<%=%>?
这样做的目的是在页面加载时从XML(通过AJAX)获取参数,将参数传递回我的JSP页面,以便我可以使用它动态创建URL(例如“ http://
xyz&Param =” + session.getAttribute(“ x”)+“”)。请注意,必须在jsp页面的Java标签<%=
....%>中定义URL。
回答:
您不能在scriptlet中使用Javascript变量。我希望您知道,JSP是在服务器端以及在进行AJAX调用之前执行的。您应该对代码进行一些调整以实现此目的,并在JS中构造URL。像这样,
在JSP中,您可以拥有
<input type='hidden' value='<%=dynamicallyCreatedURL%>' id='dynamicallyCreatedURL'/>阅读Ajax Response回调中的上述隐藏元素以构造URL。您可以在任何地方使用构造的url。在这里我用作形式动作
$(xml).find('Rowsets').each(function(){    var x = $(this).find('Auto_Id').text() // Assign data from Auto_Id into variable
    document.form.y.value = x; // Pass the parameter back to the JSP page
    //Here construct the URL and set as forma action
   var dynamicallyCreatedURL = document.getElementById('dynamicallyCreatedURL').value+'?param='+x; 
document.form.action = dynamicallyCreatedURL;
}
以上是 将参数从AJAX传递到JSP页面 的全部内容, 来源链接: utcz.com/qa/406114.html

