Javascript Ajax示例程序不工作
我试图发出一个AJAX请求(来自New.html)并在一个servlet中处理该请求(在PropertyReader.java中)。对于get请求,我只是想打印到控制台,看看是否能够在doGet()方法中获得AJAX请求,但没有打印任何内容。表示未调用doGet方法。我在html页面中单击一个按钮后发送AJAX请求。请帮帮我。我不知道我错过了什么 我的Servlet:PropertyReader.javaJavascript Ajax示例程序不工作,javascript,ajax,jakarta-ee,Javascript,Ajax,Jakarta Ee,我试图发出一个AJAX请求(来自New.html)并在一个servlet中处理该请求(在PropertyReader.java中)。对于get请求,我只是想打印到控制台,看看是否能够在doGet()方法中获得AJAX请求,但没有打印任何内容。表示未调用doGet方法。我在html页面中单击一个按钮后发送AJAX请求。请帮帮我。我不知道我错过了什么 我的Servlet:PropertyReader.java package org.jboss.samples.webservices; public
package org.jboss.samples.webservices;
public class PropertyReader extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public PropertyReader() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("************getting GET request");
}
}
我的带有AJAX调用的html页面:New.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Status Report</title>
<Script>
function getIt()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("ready");
document.getElementById("fill").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/PropertyReader?property="+document.getElementById("property").value,true);
xmlhttp.send();
alert("property2="+document.getElementById("property").value);
return false;
}
</script>
</head>
<body>
Hi !!
<a href="pdf">pdf</a>
<form >
<input id="property" type="text" width="50">
<input type="button" value="Get It!" onclick="getIt();">
</form>
<div id="fill" style="font:20px; color:red;">
who am i?
</div>
</body>
</html>
状态报告
函数getIt()
{
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
警惕(“准备就绪”);
document.getElementById(“fill”).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open(“GET”,“/PropertyReader?property=“+document.getElementById(“property”)。值,true);
xmlhttp.send();
警报(“property2=“+document.getElementById(“property”).value”);
返回false;
}
你好
我是谁?
My DD:web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TestWebb</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>New.html</welcome-file>
</welcome-file-list>
<servlet>
<display-name>HelloWorld</display-name>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>org.jboss.samples.webservices.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>FileServer</display-name>
<servlet-name>FileServer</servlet-name>
<servlet-class>org.jboss.samples.webservices.FileServer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileServer</servlet-name>
<url-pattern>/pdf</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>PropertyReader</display-name>
<servlet-name>PropertyReader</servlet-name>
<servlet-class>org.jboss.samples.webservices.PropertyReader</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PropertyReader</servlet-name>
<url-pattern>/PropertyReader/*</url-pattern>
</servlet-mapping>
</web-app>
TestWebb
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
New.html
你好世界
你好世界
org.jboss.samples.webservices.HelloWorld
你好世界
/地狱世界
文件服务器
文件服务器
org.jboss.samples.webservices.FileServer
文件服务器
/pdf
地产领头羊
地产领头羊
org.jboss.samples.webservices.PropertyReader
地产领头羊
/地产领头羊/*
可能是xmlhttp.open调用中的问题。
尝试使用绝对路径,即http://...... 用于url而不是相对url
另外,要检查ajax是否正在发生,您可以使用浏览器中的开发人员工具,方法是按F12键并监视网络部分。首先从url中删除“/”,然后可以正常工作
像
我认为如果您正确地给出了所有文件名,它将很好地工作
或者您也可以使用其他http状态检查它
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("ready");
document.getElementById("fill").innerHTML=xmlhttp.responseText;
}
else if(xmlhttp.status == 400)
{
alert ('bad status');
}
// for other status also for checks working
}
在浏览器控制台或服务器端报告的任何错误?无错误。。只是请求根本没有到达服务器!现在通过相对url的更改来解决这个问题。。谢谢:)谢谢开发者工具提示。当我监视网络部分时,AJAX请求出现404错误。我复制了链接地址,结果URL不是。然后我删除了相对URL前面的“/”,它成功了!
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("ready");
document.getElementById("fill").innerHTML=xmlhttp.responseText;
}
else if(xmlhttp.status == 400)
{
alert ('bad status');
}
// for other status also for checks working
}