开发者

XML parsing using jQuery instead of Ajax

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();


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",
    ...
});


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜