2011-07-12 9 views
3

私は、デスクトップからxmlファイルを取得し、htmlページに表示するためのJavaScriptをいくつか書きました。しかし、私は今xmlファイルをWebサーバー(mongoose)に追加しました。私はそのサーバーからファイルを呼び出したいと思うが、サーバーからファイルを呼び出すときはいつでもうまくいきませんが、デスクトップから呼び出すとうまく読み込みます。私はここでJavascriptを使用してサーバーからファイルを取得する

xmlhttp.open("GET","http://localhost:8080/Devices.xml",false); 

xmlhttp.open("GET","Devices.xml",false); 

を交換したい

はコード

<html> 
<head> 

<script type="text/javascript"> 
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
    else {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    xmlhttp.open("GET","Devices.xml",false); 
    xmlhttp.send(); 
    xmlDoc=xmlhttp.responseXML; 


    // the <Device> list 
    x = xmlDoc.getElementsByTagName('Device'); 

    // make a function that extracts the attributes out of a Node 
    function getDeviceAttributes(dvc) { 
    var name = dvc.getAttribute("name"); 
    var uuid = dvc.getAttribute("uuid"); 
    var id = dvc.getAttribute("id"); 
    return "<p>name: " + name + "<br> uuid: " + uuid + "<br> id: "+ id + "</p>"; 
    } 

    // loop through the list 
    // assuming order doesn’t matter 
    var txt = ''; 
    for (var i = x.length; i--;) { 
    txt += getDeviceAttributes(x[i]); 
    } 

    //show the result on page load 
    window.onload = function() { 
    document.getElementById("showDevices").innerHTML = txt; 
    }; 

</script> 
</head> 

<body> 

<div id='showDevices'></div> 

</body> 
</html> 

私はこの作業を取得することができますどのように誰もが知っていますか?

私はAJAXとJqueryを使用するように言われていますが、どうやってどこから始めるべきか分かりません。

+6

あなたは、同じWebサーバーからそのファイルを取得するページを提供していますか?同じ起源の問題に遭遇している可能性があります –

答えて

4

jQueryがあなたにできることはたくさんあります。このようなGet request method

だから、何かのためにドキュメントをチェックアウト:

$.get('http://localhost:8080/Devices.xml', function(data) { 
    $('#showDevices').html(data); 
}); 

私はそれが何をしようとするためのjQueryのであると信じています。希望が役立ちます。あなたが応答し、これを解析したくなかった場合

-Charlie

ただ、いくつかの一般的なアドバイス、あなたも.LOAD()Ajaxの機能を使用できます。

window.onload = function() { 
    document.getElementById("showDevices").innerHTML = txt; 
}; 

はで行うことができますこのようなjQuery $("#showDevices").html(txt);

関連する問題