Jquery – 解析从URL接收的XML

我有这个URL,我应该从中接收XML。 到目前为止我有这个:

function GetLocationList(searchString) { $.ajax({ url: "http://konkurrence.rejseplanen.dk/bin/rest.exe/location?input=" + searchString, type: "GET", dataType: "html", success: function(data) { //Use received data here. alert("test"); } }); 

试图用firebug进行调试,但它没有进入成功方法。 虽然,在DreamWeaver中,它能够发布一个简单的警报,它在成功方法中。 我尝试将xml编写为dataType,但在编写alert(数据)时它(在DreamWeaver中)不起作用。 但是,当我将html编写为dataType时,它会显示整个XML的警报。

如何正确获取XML,以及如何解析并获取“StopLocation”元素?

尝试添加错误function。

请在此处输入链接描述

这将为您提供使用Firefox调试代码所需的所有信息。

 $.ajax({ url: "http://konkurrence.rejseplanen.dk/bin/rest.exe/location?input=" + searchString, type: "GET", dataType: "html", success: function(data) { //Use received data here. alert("test"); }, error: function(jqXHR, textStatus, errorThrown ){ // debug here } }); 

您需要先解析它,然后才能搜索属性。 像这样。

 success: function(data) { var xml = $.parseXML(data) $(xml).find('StopLocation').each(function() { var name = $(this).attr('name'); alert(name); } ); 

这将为您提供每个StopLocation的名称。

希望这会有所帮助,您也可以对文档中的所有其他属性使用相同的方法。