2011-04-05 31 views
1

我試圖用jquery獲得一個asp生成的xml文件,按照read xml by jquery,但我得到status = parsererror err = TypeError:data爲null。將xml文件輸入瀏覽器會生成完美的xml文件。我希望我沒有做的wget ...jquery從asp檢索xml的問題

xmlhttp = "http://10.1.10.154:1014/dtsearch_2.asp?cmd=pdfhits&DocId=44&Index=C%3a%5cstream%5cSupport%5cIDX%5fESTATE&HitCount=4&hits=185+1ac+1d5+1ff+&hc=323&req=knife" 

$.ajax({ 
    url: xmlhttp, 
    method: "POST", 
    dataType: "xml", 
    success: function(data) { 
     var xdoc = $(data); // Note that jQuery has already done the parsing for us 
     alert("Getting tutorials"); 
     var tutorials = xdoc.find("loc"); 
     alert("Found: " + tutorials.length); 
     tutorials.each(function() { 
      alert("Tutoral author: " + $(this).attr("pg")); 
     }); 
    }, 
    error: function(jxhr, status, err) { 
     alert("Ajax error: status = " + status + ", err = " + err); 
    } 
}); 

我使用本地IP地址和端口1014,但我不明白爲什麼所有的會事......

謝謝, 尼克

回答

2

取出var xdoc = $(data),只是直接引用data

$.ajax({ 
    url: xmlhttp, 
    method: "POST", 
    dataType: "xml", 
    success: function(data) { 
     alert("Getting tutorials"); 
     var tutorials = data.find("loc"); 
     alert("Found: " + tutorials.length); 
     tutorials.each(function() { 
      alert("Tutoral author: " + $(this).attr("pg")); 
     }); 
    }, 
    error: function(jxhr, status, err) { 
     alert("Ajax error: status = " + status + ", err = " + err); 
    } 
}); 

編輯

這符合了Same Origin Policy。如果你需要它在一個單獨的端口上,你將需要使用JSONP。

As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.

$.ajax({ 
    url: xmlhttp, 
    method: 'POST', 
    dataType: 'jsonp xml', 
    success: function(data) { 
     alert('Getting tutorials'); 
     var tutorials = data.find('loc'); 
     alert('Found: ' + tutorials.length); 
     tutorials.each(function() { 
      alert('Tutorial author: ' + $(this).attr('pg')); 
     }); 
    }, 
    error: function() { 
     alert('Ajax error: status = ' + status + ', err = ' + err); 
    } 
}); 

參考http://api.jquery.com/jQuery.ajax/

+0

羅,數據爲空。當我螢火蟲時,我沒有迴應,但XML是 XML分析錯誤:找不到元素位置:moz-nullprincipal:{1ab824d6-817a-4c37-8256-893e5da80577}行號1,列1: ^ – Nick 2011-04-05 02:26:39

+0

Can你張貼你的XML?試圖重新創建,但我無法訪問XML文件。 – jon3laze 2011-04-05 02:32:00

+0

想通了。這是港口。你不能用ajax指定一個端口嗎? – Nick 2011-04-05 02:33:24