Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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 在使用JSP时,我不会在ajax程序中获得输出_Javascript_Ajax_Jsp - Fatal编程技术网

Javascript 在使用JSP时,我不会在ajax程序中获得输出

Javascript 在使用JSP时,我不会在ajax程序中获得输出,javascript,ajax,jsp,Javascript,Ajax,Jsp,我试图使用ajax获得动态输出,比如“如果有人添加了食物名称,那么ajax在后台工作,并动态地给我输出,无论食物是否在数组中,并给出相应的消息”。但我没有得到这一点。。请参阅下图: Html文件:: '<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Foodstore</title> <script type="text/javascri

我试图使用ajax获得动态输出,比如“如果有人添加了食物名称,那么ajax在后台工作,并动态地给我输出,无论食物是否在数组中,并给出相应的消息”。但我没有得到这一点。。请参阅下图:

Html文件::

'<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Foodstore</title>
<script type="text/javascript" src="foodstore.js"></script>
</head>
<body onload="process()">
<h3> The FoodStore</h3>
Enter the food you want :
<input type="text" id="userInput"/>
<div id="underInput" />
</body>
</html>`
'
食品店
食品店
输入您想要的食物:
`
Java脚本::

`/**
* 
*/

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject(){
var xmlHttp;
if(window.ActiveXObject){

try{

     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
    catch (e) {
        // TODO: handle exception
         xmlHttp = false;
    }
}

else{
try{

     xmlHttp = new XMLHttpRequest;
    }
    catch (e) {
        // TODO: handle exception
         xmlHttp = false;
    }
}

if(!xmlHttp)
    alert("Sorry cannot creat object");
else
    return xmlHttp;
}

function process(){

if(xmlHttp.readyState==0 || xmlHttp.readyState==4){

    var food=encodeURIComponent(document.getElementById("userInput").value);

    xmlHttp.onreadystatechange= handleServerResponse;
    xmlHttp.open("GET","foodstore.jsp?food=" +food,true);
    xmlHttp.send(null);
}else{

    setTimeout('process()', 1000);
}
}

function handleServerResponse(){

if(xmlHttp.readyState==4){
    if(xmlHttp.status=200){

        var xmlResponse = xmlHttp.responseXML;
        var xmlDocumentElement = xmlResponse.documentElement;
        var message = xmlDocumentElement.firstChild.data;

        document.getElementById("underInput").innerHTML = '<span           style="color:blue">' 
        + message + '</span>';
        setTimeout('process()', 1000);
    }else {
        alert('Something went wrong!');
    }
}
}`
`/**
* 
*/
var xmlHttp=createXmlHttpRequestObject();
函数createXmlHttpRequestObject(){
var-xmlHttp;
if(window.ActiveXObject){
试一试{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}
捕获(e){
//TODO:处理异常
xmlHttp=false;
}
}
否则{
试一试{
xmlHttp=新的XMLHttpRequest;
}
捕获(e){
//TODO:处理异常
xmlHttp=false;
}
}
如果(!xmlHttp)
警报(“抱歉,无法创建对象”);
其他的
返回xmlHttp;
}
函数过程(){
if(xmlHttp.readyState==0 | | xmlHttp.readyState==4){
var food=encodeURIComponent(document.getElementById(“userInput”).value);
onreadystatechange=handleServerResponse;
open(“GET”,“foodstore.jsp?food=“+food,true”);
xmlHttp.send(空);
}否则{
setTimeout('process()',1000);
}
}
函数handleServerResponse(){
if(xmlHttp.readyState==4){
if(xmlHttp.status=200){
var xmlResponse=xmlHttp.responseXML;
var xmlDocumentElement=xmlResponse.documentElement;
var message=xmlDocumentElement.firstChild.data;
document.getElementById(“underInput”).innerHTML=''
+信息+“”;
setTimeout('process()',1000);
}否则{
警惕(“出了什么事!”);
}
}
}`
jsp::

`
`

提前感谢:)

你为什么这么辛苦?您不允许使用jQuery吗?
jQuery
是选项吗?对不起,我是初学者,刚刚开始学习ajax。。所以我只是在做实验,而且我还没有学会jquery。。因此,使用JSP确保您在
WebContent
文件夹下拥有上述所有文件,并且
JSTL
jar包含在
WEB-INF/lib
中。非常感谢。我用过wamp程序不起作用。但在把它放到新项目的webcontent中并使用Eclips-EE之后,它就成功了!!再次感谢:)
`<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<%@ page contentType="text/xml" %>

<response>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<c:set var="flag" value="0"></c:set>
<c:set var="s" value="${param.food }"></c:set>
<c:set var="a" value="${fn:split('grapes,apple,banana', ',')}"       scope="application" />
<c:forEach var="i" begin="0" end="2">
<c:if test="${fn:contains(a[i],s) }">
<c:set var="flag" value="1"></c:set>
</c:if>
</c:forEach>

<c:if test="${flag eq 0 }">
<c:out value="Sorry We do not have  ${s} !!"></c:out>
</c:if>

<c:if test="${param.food eq null }">
<c:out value="Plese Enter Food name!!!"></c:out>
</c:if>

<c:if test="${flag eq 1 }">
<c:out value="Yes We Do have ${s} !"></c:out>
</c:if>

</response>    `