将参数从 AJAX 传递到 JSP 页面
我正在尝试将参数从 AJAX 传递回我的 JSP 页面。这是我的示例代码:
JS 文件:
$(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
});
}
});
});
.JSP 文件:
<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") + "")。请注意,URL 必须在 java 标签 <%= .... %> 中定义。 jsp页面的。
I am trying to pass a parameter from AJAX back to my JSP page. Here is my sample code:
JS File:
$(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
});
}
});
});
.JSP File:
<FORM name="form"><input name="y" value="" /></FORM> //value left blank for AJAX to auto-populate
The above code works - I am able to get the parameter x. However, is it possible to get the value of x into the following format on the same .JSP page?
<%= session.getAttribute("x") %>
Or, get the value of x and pass it into the java tags <%= %>?
The purpose of this is to grab the parameter from the XML (via AJAX) on page load, pass a parameter back to my JSP page so that I can use it to dynamically create a URL (e.g. "http://xyz&Param=" + session.getAttribute("x") + ""). Note that the URL has to be defined in java tags <%= .... %> of the jsp page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在 scriptlet 中使用 Javascript 变量。我希望您知道,JSP 是在服务器端执行 AJAX 调用之前执行的。您应该在代码中做一些调整来实现这一点,在 JS 中构造 URL。像这样,
在JSP中,您可以,
读取Ajax响应回调中的上述隐藏元素来构造URL。您可以在任何地方使用构造的 url。这里我用作表单动作
You can't use Javascript variable in scriptlets. I hope you know that, JSPs are executed at server side and before making your AJAX call. You should do some tweaks in your code to achive this, construct the URL in JS. Like this,
In JSP, You can have,
Read the above hidden element in Ajax Response callback to construct the URL. You can use constructed url in anywhere. Here I used as form action