使用 jQuery 而不是 Ajax 进行 XML 解析

发布于 2024-10-24 17:39:54 字数 1274 浏览 1 评论 0原文

我试图使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身狗的梦 2024-10-31 17:39:54

由于同源策略限制,您无法将 AJAX 请求发送到远程域,因此这无法工作,除非包含此 JavaScript 的页面托管在 http://survey-creator.appspot.com 上。我怀疑您正在尝试获取托管在不同域上的 XML 文档,这是不可能的。

如果您想执行此操作,您可能需要在您的域上使用服务器端脚本,该脚本将执行远程调用来获取 XML,然后返回此 XML,以便您的 AJAX 调用调用此服务器脚本:

$.ajax({
    type: "GET",
    url: "/myscript",
    ...
});

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:

$.ajax({
    type: "GET",
    url: "/myscript",
    ...
});
岁吢 2024-10-31 17:39:54

看起来(基于代码片段)您有两种情况,其中 });} 行的顺序颠倒了。

你发帖的时候有把东西拿走吗?如果没有,那么看看当格式正确时这些问题如何变得清晰。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文