我想发送我通过ajax请求定义的JSP java变量,这可能吗?
我的 JSP:
<%
String fileName = (String)request.getAttribute(MatafConstants.PARAM_IMG_FILE_NAMES);
%>
然后我想发送这个字符串:
$.ajax({
url: "/some/urlServlet",
type: "get", //send it through get method
data: {
"statusID": status,
"test": '<%=fileName%>' //here is the param I send
},
success: function(response) {
//Do Something
console.log(response);
},
error: function(xhr) {
//Do Something to handle error
}
});
我如何发送这个参数?或者更大的问题我可以在 JavaScript 中使用 JSP java 变量吗?
最佳答案
您可以使用 scriplet 中的值创建隐藏字段,然后在 Java 脚本中使用它的值。如下所示:
JSP代码
<input type="hidden" id="fileName" value="<%=(String)request.getAttribute(MatafConstants.PARAM_IMG_FILE_NAMES)%>">
Java脚本代码
data: {
"statusID": status,
"test": document.getElementById("fileName").value;
},
关于java - JSP通过ajax发送Java变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44516657/