I am trying to use Java-script as a client for java based restful web-service. The service is a survey maker. I am having trouble getting the function to work. The server side of the service is in Google App Engine. In the code below the function uses http get to get xml representing a list of surveynames, then gets the data from the xml and puts it in a html table. The code is not working, so it would be great if some one could check it to see if I am doing this correctly or I am doing something wrong. I have never programed in javascript so I would also like to know if I need to import a library to use AJAX or is it supported by the browser?
我正在尝试使用java脚本作为基于java的restful web服务的客户端。这项服务是一个调查机构。我在使这个函数工作时遇到了麻烦。服务的服务器端在谷歌应用程序引擎中。在下面的代码中,函数使用http get获取表示surveynames列表的xml,然后从xml获取数据并将其放入html表中。代码不起作用,所以如果有人可以检查一下,看看我做的是否正确,或者我做错了什么,那就太好了。我从来没有编程过javascript,所以我想知道我是否需要导入一个库来使用AJAX,还是需要浏览器支持?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>View Surveys</title>
</head>
<SCRIPT>
function getSurveyNames(){
var url = "http://survey-creator.appspot.com/rest/surveymakerpro/allsurveys";
var xmlhttp;
// AJAX code for Mozilla, Safari, Opera etc.
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = xmlhttpChange;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}
// AJAX code for IE
else
if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
if (xmlhttp) {
xmlhttp.onreadystatechange = xmlhttpChange;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
}
}
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
HTMLSurveyNames = "<table border='1'><tr>Survey Names<th></th></tr>";
var surveyNames = xmlhttp.responseXML.documentElement.getElementsByTagName("surveys")[0];
for(var i = 0; i < surveyNames.length ;i++){
var surveyNameChildNode = surveyName[i].childNodes[0];
var name = surveyNameChildNode.nodeValue;
HTMLSurveyNames += "<tr><td>"+name+"</td></tr>";
}
//div tags
document.getElementById('displayNames').innerHTML = HTMLSurveyNames;
}
}
</SCRIPT>
<body>
<p>View Survey</p>
<form method="post">
<input name="GetSurveys" style="width: 103px" type="button" value="View all surveys" onClick=getSurveyNames(); /></form>
<p>Here Goes a Table of Surveys</p>
<div id="displayNames">
<p>Enter the survey you wish to take:</p>
<form method="post">
<input id="surveyName" name="SurveyName" style="width: 140px" type="text" value="Enter Survey Name...." /></form>
<form method="post">
<input name="Submit2" type="submit" value="Get Survey" /></form>
<div id="displaySurvey"></div>
</div>
<p>
<input id="sendtoserver" name="Submit3" type="submit" value="Submit TakenSurvey" /></p>
</body>
</html>
This is the xml I want to parse
这就是我要解析的xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><surveyNames><SurveyList><surveys>DragonBallZ</surveys><surveys>FootballSurvey</surveys><surveys>NewsSurvey</surveys><surveys>PennstateSurvey</surveys></SurveyList></surveyNames>
2 个解决方案
#1
0
-
You're sending off an asynchronous request, but then attempting to immediately process the result before this request will have finished.
您正在发送一个异步请求,但在此请求完成之前,试图立即处理结果。
-
You should assign a handler to
xmlhttp.onreadystatechange
that will be executed as your request progresses. You currently assignxmlhttpChange
to this property, but you don't show whatxmlhttpChange
is. You should be doing something like this:您应该为xmlhttp分配一个处理程序。onreadystatechange将在您的请求进行时执行。您当前将xmlhttpChange分配给此属性,但不显示xmlhttpChange是什么。你应该这样做:
xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // XML parsing code goes here } }
-
You do not need to import any libraries to use Ajax
使用Ajax不需要导入任何库
-
Be careful with lines like
HTMLSurveyNames = "<table border='1'><tr>Survey Names<th></th></tr>";
You should always use thevar
keyword when declaring variables, to avoid creating/modifying globals implicitly.注意HTMLSurveyNames = "
Survey name ";在声明变量时,应该始终使用var关键字,以避免隐式地创建/修改全局变量。
#2
0
"<table border='1'><tr>Survey Names<th></th></tr>"
should be
应该是
"<table border='1'><tr><th>Survey Names</th></tr>"
things will work a LOT better.
事情会好得多。
there is a cross-browser XML librari for javascript at http://www.softxml.com/softxmllib/softxmllib.htm I believe XMLHTTPRequest() is specific to IE. there are versions for other browsers. http://en.wikipedia.org/wiki/XMLHttpRequest
在http://www.softxml.com/softxmllib/softxmllib.htm我认为XMLHTTPRequest()是IE特有的。还有其他浏览器的版本。http://en.wikipedia.org/wiki/XMLHttpRequest
#1
0
-
You're sending off an asynchronous request, but then attempting to immediately process the result before this request will have finished.
您正在发送一个异步请求,但在此请求完成之前,试图立即处理结果。
-
You should assign a handler to
xmlhttp.onreadystatechange
that will be executed as your request progresses. You currently assignxmlhttpChange
to this property, but you don't show whatxmlhttpChange
is. You should be doing something like this:您应该为xmlhttp分配一个处理程序。onreadystatechange将在您的请求进行时执行。您当前将xmlhttpChange分配给此属性,但不显示xmlhttpChange是什么。你应该这样做:
xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // XML parsing code goes here } }
-
You do not need to import any libraries to use Ajax
使用Ajax不需要导入任何库
-
Be careful with lines like
HTMLSurveyNames = "<table border='1'><tr>Survey Names<th></th></tr>";
You should always use thevar
keyword when declaring variables, to avoid creating/modifying globals implicitly.注意HTMLSurveyNames = "
Survey name ";在声明变量时,应该始终使用var关键字,以避免隐式地创建/修改全局变量。
#2
0
"<table border='1'><tr>Survey Names<th></th></tr>"
should be
应该是
"<table border='1'><tr><th>Survey Names</th></tr>"
things will work a LOT better.
事情会好得多。
there is a cross-browser XML librari for javascript at http://www.softxml.com/softxmllib/softxmllib.htm I believe XMLHTTPRequest() is specific to IE. there are versions for other browsers. http://en.wikipedia.org/wiki/XMLHttpRequest
在http://www.softxml.com/softxmllib/softxmllib.htm我认为XMLHTTPRequest()是IE特有的。还有其他浏览器的版本。http://en.wikipedia.org/wiki/XMLHttpRequest