2010-05-17 114 views
3

我正在使用jQuery並嘗試加載一個變量來代替命名的xml文件。 我的代碼:在XMLHttpRequest中使用變量名稱

$(document).ready(function() { 
     // bind 'myForm' and provide a simple callback function 
     $('#theForm').ajaxForm(function(responseXML2) { 

      var myxml = responseXML2; 
      alert(responseXML2); 
      displayResult(); 


      }); 
}); 
function loadXMLDoc(dname) 
{ 
if (window.XMLHttpRequest) 
    { 
    alert("loading xmlhttprequest"); 
    xhttp=new XMLHttpRequest(); 
    } 
else 
    { 
    alert("loading activeX"); 
    xhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
alert("bottom load"); 
xhttp.open("GET",dname,false); 
xhttp.send(); 
return xhttp.responseXML; 
} 

function displayResult() 
{ 
alert("setting vars"); 

alert("displayResult called"); 

//xml=loadXMLDoc(responseXML2); //tried this and the line below, among others 
xml=responseXML2; 
alert("xmlDocLoaded"); 
xsl=loadXMLDoc("xslt-test.xsl"); 
alert("XSLloaded"); 
// code for IE 
if (window.ActiveXObject) 
    { 
    alert("IE"); 
    ex=xml.transformNode(xsl); 
    document.getElementById("ieiresponse").innerHTML=ex; 
    } 
// code for Mozilla, Firefox, Opera, etc. 
else if (document.implementation && document.implementation.createDocument) 
    { 
    alert("notIE"); 
    xsltProcessor=new XSLTProcessor(); 
    xsltProcessor.importStylesheet(xsl); 
    resultDocument = xsltProcessor.transformToFragment(xml,document); 
    document.getElementById("ieiresponse").appendChild(resultDocument); 
    } 
} 

在上面的代碼我想有:

//xml=loadXMLDoc(responseXML2); //tried this and the line below, among others 
xml=responseXML2; 

,而不是一個文件名爲:

xsl=loadXMLDoc("example.xml"); 

當我通過代碼運行,它的工作原理,如果我給這個文件起了名字,但是當我使用這個變量的時候(它出現在警報中,所以被拉出來),它會停止上面的代碼(把變量放在xml文件中)

任何幫助將不勝感激!先謝謝你。

+0

你有jQuery並做類似'if(window.XMLHttpRequest)'的事情嗎?爲什麼?! – Tomalak 2010-05-17 19:01:56

+0

我使用jQuery來檢索表單發佈的XML響應。如果有更好的方法(我是XML新手),我非常開放! – Paul 2010-05-17 19:09:21

+0

我正在查看更多的代碼,它看起來像responseXML2沒有被拉入新的功能。我試圖把現有的displayresult()代碼底部在這裏下: $( '#theForm')給ajaxForm(函數(responseXML2){ VAR myxml = responseXML2; 警報(responseXML2); 配售代碼在這裏 沒有運氣,現在它甚至沒有調用警報! – Paul 2010-05-17 19:10:47

回答

2

從評論:

我基本上是想表單POST到服務器, 接收響應回XML,應用XSLT 到XML,並在頁面上的一個div顯示它。

從我所看到的,這樣的事情應該已經做你想要的一切:

$(document).ready(function() { 
    // prepare sending the AJAX form (use ajaxSubmit() to actually send it) 
    $('#theForm').ajaxForm({ 
    dataType: 'xml', 
    success: function(responseText, statusText, xhr, $form) { 
     // jQuery xslt plugin required for this: 
     $.xslt({ 
     xml: xhr.responseXML, 
     xslUrl: "xslt-test.xsl", 
     target: "#ieiresponse" 
     }); 
    }, 
    error: function(xhr, textStatus, errorThrown) { 
     alert("Oops, there was an error: " + textStatus); 
    } 
    }); 
}); 

你的代碼是非常充滿了事情的jQuery已經爲您做的(如選擇取決於正確的XmlHttpRequest對象在瀏覽器類型和其他東西)。你可以也應該擺脫所有這些。你應該開始閱讀一些jQuery教程,因爲即使你說的不同,你的代碼並不表示在你所有的

+0

嗨Tomalek。 非常感謝您的代碼。我正在嘗試這個,但是當我點擊表單提交按鈕時,沒有任何反應。我之前在xslt中遇到過這個問題。也許這是我在引用xslt.js時做錯了什麼? – Paul 2010-05-17 19:56:09

+0

這是我參考js的代碼: Paul 2010-05-17 19:56:51

+0

與我一起嘗試xslt.js和jquery.xslt.js在那裏。通常它只是一個。 – Paul 2010-05-17 19:57:17

0

好了,所以我回答我自己的問題: 這裏是別人

1)我嵌套我的功能,所以它實際上引用變量的結果。 2)I代替

xml=loadXMLDoc(responseXML2); 

與:

xml=responseXML2; 

現在嵌套函數內工作。