使用 jQuery 而不是 Ajax 进行 XML 解析
我试图使用 Ajax 来解析 XML,但它没有成功,所以我使用了 jQuery 库,它看起来使用起来更简单。我正在努力为客户提供休息服务。服务输出 XML,客户端应该解析它并将其显示在表中。我不知道我做错了什么,该功能似乎无法正常工作。如果我能得到一点指导,我将不胜感激。
这就是我调用 jQuery 的方式:
script src="http://code.jquery.com/jquery-1.5.1.js" type="text/javascript"
这是代码:
var HTMLSurveyNames;
function getSurveyNames(){
alert("hery");
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://survey-creator.appspot.com/rest/surveymakerpro/allsurveys",
dataType: "xml",
success: function(xml) {
HTMLSurveyNames = "<table border='1'><tr>Survey Names<th></th></tr>";
$(xml).find('SurveyList').each(function(){
var surveyName = $(this).find('surveys').text();
HTMLSurveyNames += "<tr><td>"+surveyName+"</td></tr>";
});
document.getElementById('displayNames').innerHTML = HTMLSurveyNames;
});
}
});
});
}
这是我希望表格出现的位置:
div id="displayNames"
这是对函数的调用:
input name="GetSurveys"
style="width: 103px"
type="button" value="View all surveys"
onClick=getSurveyNames();
I was trying to use Ajax to parse XML but it was not working out, so I used the jQuery library which seems a lot simpler to use. I am trying to make a client for a rest service. The service spits out XML and the client should parse it and display it in a table. I don't know what I'am doing wrong the function does not seem to work at out. I would appreciate it if I could get a little bit of guidance.
This is how I call jQuery:
script src="http://code.jquery.com/jquery-1.5.1.js" type="text/javascript"
This is the code:
var HTMLSurveyNames;
function getSurveyNames(){
alert("hery");
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://survey-creator.appspot.com/rest/surveymakerpro/allsurveys",
dataType: "xml",
success: function(xml) {
HTMLSurveyNames = "<table border='1'><tr>Survey Names<th></th></tr>";
$(xml).find('SurveyList').each(function(){
var surveyName = $(this).find('surveys').text();
HTMLSurveyNames += "<tr><td>"+surveyName+"</td></tr>";
});
document.getElementById('displayNames').innerHTML = HTMLSurveyNames;
});
}
});
});
}
This is where I would want the table to appear:
div id="displayNames"
and this is the call to the function:
input name="GetSurveys"
style="width: 103px"
type="button" value="View all surveys"
onClick=getSurveyNames();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于同源策略限制,您无法将 AJAX 请求发送到远程域,因此这无法工作,除非包含此 JavaScript 的页面托管在
http://survey-creator.appspot.com
上。我怀疑您正在尝试获取托管在不同域上的 XML 文档,这是不可能的。如果您想执行此操作,您可能需要在您的域上使用服务器端脚本,该脚本将执行远程调用来获取 XML,然后返回此 XML,以便您的 AJAX 调用调用此服务器脚本:
Due to same origin policy restrictions you cannot send AJAX requests to distant domains, so this cannot work unless the page containing this javascript is hosted on
http://survey-creator.appspot.com
. I suspect that you are trying to fetch an XML document which is hosted on a different domain which is impossible.If you want to do this you might need to use a server side script on your domain which will do the remote call to fetch the XML and then return this XML so that your AJAX call invokes this server script:
看起来(基于代码片段)您有两种情况,其中
});
和}
行的顺序颠倒了。你发帖的时候有把东西拿走吗?如果没有,那么看看当格式正确时这些问题如何变得清晰。
It looks like (based on the code snippet) that you have two cases where the order of
});
and}
lines were reversed.Did you take stuff out when you posted? If not then see how when formated correctly these problems become clear.