Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在不使用jQuery的情况下从jsp获取到ajax的图像url_Javascript_Ajax_Jsp - Fatal编程技术网

Javascript 在不使用jQuery的情况下从jsp获取到ajax的图像url

Javascript 在不使用jQuery的情况下从jsp获取到ajax的图像url,javascript,ajax,jsp,Javascript,Ajax,Jsp,我在javascript中有一个函数,每次我都想显示一个不同的图像:它从jsp(通过一个方法与数据库连接)获取这个图像 函数myFunction(eventId){ xmlhttp=新的XMLHttpRequest() 以下是jsp代码: <% String ev = request.getParameter("e"); int eventt = Integer.parseInt(ev); Ev e = new Ev(); String img = e.g

我在javascript中有一个函数,每次我都想显示一个不同的图像:它从jsp(通过一个方法与数据库连接)获取这个图像

函数myFunction(eventId){ xmlhttp=新的XMLHttpRequest()

以下是jsp代码:

<% 
    String ev = request.getParameter("e");
    int eventt = Integer.parseInt(ev);
    Ev e = new Ev();
    String img = e.getPicture(eventt);
    response.setContentType("image/jpg");  
%>

我不确定响应服务器(jsp)返回到客户端(javascript)的方式是否正确
(从数据库获取url的方法是正确的)

首先,为图像使用正确的内容类型。它是
image/jpeg
,还有第二个
e

然后:不要使用XMLHttpRequest

您希望在页面中显示图像,而不需要直接访问JS中的原始图像数据

document.getElementById("imgevent").style.background =
    "url(pictures.jsp?e=" + eventId + ") repeat scroll 0 0 / cover";

首先,为图像使用正确的内容类型。它是
image/jpeg
,还有第二个
e

然后:不要使用XMLHttpRequest

您希望在页面中显示图像,而不需要直接访问JS中的原始图像数据

document.getElementById("imgevent").style.background =
    "url(pictures.jsp?e=" + eventId + ") repeat scroll 0 0 / cover";

您不需要异步请求,只需添加url作为源:

<img id="myimg" src="pictures.jsp?e=0">
<script>
var img;
window.onload=function(){
img=document.getElementById("myimg");
 }
 function changeimg(eid){
 img.src="pictures.jsp?e="+eid;
 }
 </script>

var-img;
window.onload=function(){
img=document.getElementById(“myimg”);
}
功能变更(eid){
img.src=“pictures.jsp?e=“+eid;
}

您不需要异步请求,只需添加url作为源:

<img id="myimg" src="pictures.jsp?e=0">
<script>
var img;
window.onload=function(){
img=document.getElementById("myimg");
 }
 function changeimg(eid){
 img.src="pictures.jsp?e="+eid;
 }
 </script>

var-img;
window.onload=function(){
img=document.getElementById(“myimg”);
}
功能变更(eid){
img.src=“pictures.jsp?e=“+eid;
}

这意味着我不必使用xmlhttp.open(“GET”,“eventPictures.jsp?eventId=“+eventId,true”);xmlhttp.send();?是的。因此“不要使用XMLHttpRequest”!我对jsp和response.setContentType(“image/jpeg”)的使用不是很确定。我希望它能满足您的建议,谢谢!function myFunction(eventId){document.getElementById)(“imgevent”).style.background=“url(eventPictures.jsp?eventId=“+eventId+”)重复滚动0/cover”}我试过你说的,但仍然不起作用。也许我从数据库中得到的字符串img像这样'img/event1.jpg'是错误的?@maria-你是说JSP的输出是字符串而不是图像吗?所以它应该有内容类型
text/plain
?这意味着我不必使用xmlhttp.open(“get”,“eventPictures.jsp?eventId=“+eventId,true”);xmlhttp.send();?是的。因此“不要使用XMLHttpRequest”!我对jsp和response.setContentType(“image/jpeg”);的使用不是很确定。我希望它能满足您的建议,谢谢!函数myFunction(eventId){document.getElementById(“imgevent”)。style.background=“url(eventPictures.jsp?eventId=“+eventId+”)重复滚动0/cover”;}我试过你说的,但仍然不起作用..也许我从数据库中得到的字符串img像这样'img/event1.jpg'是错误的?@maria-你是说JSP的输出是一个字符串而不是一个图像吗?所以它应该有内容类型
text/plain
?我已经在JSP onload=“myFunction()。我会试试你的方法。谢谢!我已经在jsp onload=“myFunction()。我会试试你的方法。谢谢!