我用Eclipse创建一个DWR项目。代码如下:
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
public class AJAXtest {
public AJAXtest()
{}
public List<String> getMenuItems () throws IOException
{
List<String> menuItems=new Vector<String>();
InputStream is=this.getClass().getClassLoader().getResourceAsStream("dwrapplication.properties");
Properties appProps=new Properties();
appProps.load(is);
is.close();
for (int menucount=1;true;menucount++)
{
String menuItem=appProps.getProperty("menu."+menucount);
if (menuItem==null)
{
break;
}
menuItems.add(menuItem);
}
return menuItems;
}
public String SayHello (String data)
{
return "Hello:"+data;
}
}
在eclipse中开始运行后,我可以访问页面http://localhost:8080/TestAJAX/dwr/index.html,其中显示了SayHello方法。 但是当我创建一个客户端html文件并使用IE打开它时,它显示错误消息“访问被拒绝”。 以下是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">
<link href="styles.css" rel="stylesheet" type="text/css"/>
<script type='text/javascript' src='http://localhost:8080/TestAJAX/dwr/engine.js'></script>
<script type='text/javascript' src='http://localhost:8080/TestAJAX/dwr/interface/AJAXtest.js'></script>
<script type='text/javascript' src='http://localhost:8080/TestAJAX/dwr/util.js'></script>
<title>DWR samples</title>
<script type="text/javascript">
var sayHello = function(){
return AJAXtest.SayHello("Rongbo_J",callBack);
}
var callBack = function(data)
{
alert(data);
}
</script>
</head>
<body >
<h1>DWR Easy Java Ajax Applications1</h1>
<button id="button1" type="button" onclick="sayHello()">Click Me!</button>
</body>
</html>
有人可以帮忙吗?我尝试对其进行调试,发现在engine.js中的以下位置发生了错误
try {
batch.req.open(httpMethod, request.url, batch.async);
try {
for (var prop in batch.headers) {
var value = batch.headers[prop];
if (typeof value == "string") {
batch.req.setRequestHeader(prop, value);
}
}
if (!batch.headers["Content-Type"]) {
batch.req.setRequestHeader("Content-Type", "text/plain");
}
}
catch (ex) {
dwr.engine._handleWarning(batch, ex);
}