透過JS跨網域存取XML檔

為了一些安全性的因素,通常伺服器會阻擋跨網域的XML存取,當然如果你是該網站開發者,所開發的頁面應該將會被放在被認可的網域中,不會碰到這個問題,但我們在開發前期有可能會不在所屬的網域,這樣就會造成開發上的困難,不過我們可以透過Yahoo所提供的YQL查詢語法來解決這個問題,以下就是相關的說明:

若我們在自己的電腦用以下的語法去存取一個網路上的XML

<script>
    $(function () {
        var xmlSource = "http://stanley.cloudlab.tw/crossDomainXML/testXML.xml";
        $.ajax({
            url: xmlSource,
            success: function (data) {
                console.log(data);
            }
        })
    })
</script>

很可能會出現「No Access Control Allow Origin」的訊息:

Cross doamin XML access

但如果套用上YQL之後

<script>
$(function(){
	var xmlSource = "http://stanley.cloudlab.tw/crossDomainXML/testXML.xml";
	var yqlURL = [
		"http://query.yahooapis.com/v1/public/yql",
		"?q=" + encodeURIComponent("select * from xml where url='" + xmlSource + "'"),
		"&format=xml&callback=?"
	].join("");

	$.getJSON(yqlURL, function(data){
		console.log(data)
	});
	
})
</script>
Cross doamin XML access

可以看到已經可以抓到XML內容,這樣可以讓我們較為方便的去開發,其他詳細的使用說明可以參考Yahoo YQL Guide

You may also like...

43,771 Responses

  1. Juliovophy表示:

    zithromax antibiotic: can you drink with zithromax buy zithromax

  2. BrianRof表示:

    https://aviatormocambique.site/# como jogar aviator

  3. Jdlijn表示:

    ciprofloxacin oral – septra oral buy clavulanate pills

  4. WilliamSnado表示:

    aviator game: aviator betting game – aviator game online

  5. WilliamSnado表示:

    estrela bet aviator: aviator bet – aviator jogo

  6. BrianRof表示:

    http://aviatorjogar.online/# jogar aviator Brasil

  7. Juliovophy表示:

    can you buy zithromax over the counter in canada: order zithromax zithromax online usa

  8. WilliamSnado表示:

    estrela bet aviator: aviator jogar – pin up aviator

  9. WilliamSnado表示:

    aviator: aviator oyna slot – aviator oyunu

  10. Juliovophy表示:

    zithromax capsules – https://azithromycin.pro/zithromax-vs-doxycycline.html zithromax capsules price

  11. Juliovophy表示:

    zithromax 250 price: azithromycin zithromax 500mg price generic zithromax 500mg

  12. Thomasrib表示:

    aviator oyna slot: aviator bahis – aviator sinyal hilesi

  13. Thomasrib表示:

    aviator bet: aviator online – aviator mz

  14. HowardPlomy表示:

    pin up aviator: pin-up – pin up aviator

  15. HowardPlomy表示:

    site de apostas: ganhar dinheiro jogando – jogos que dao dinheiro

  16. Twddsh表示:

    purchase ciprofloxacin generic – buy cephalexin 250mg without prescription clavulanate pill

  17. WilliamSnado表示:

    como jogar aviator em moçambique: jogar aviator – aviator moçambique

  18. WilliamSnado表示:

    aviator bet: play aviator – aviator game

  19. BrianRof表示:

    https://jogodeaposta.fun/# melhor jogo de aposta para ganhar dinheiro

  20. Thomasrib表示:

    aviator game: pin up aviator – aviator betano

  21. Thomasrib表示:

    jogo de aposta online: aplicativo de aposta – deposito minimo 1 real

  22. BrianRof表示:

    http://pinupcassino.pro/# aviator pin up casino

  23. HowardPlomy表示:

    pin up aviator: aviator – aviator oyna

  24. StephenDeery表示:

    Do you have a spam issue on this blog; I also am a blogger, and I was wondering your situation; we have created some nice procedures and we are looking to trade methods with other folks, why not shoot me an e-mail if interested.

    https://www.google.com.fj/url?q=https://hottelecom.biz/hi/

  25. Thomasrib表示:

    aviator jogo: jogar aviator Brasil – jogar aviator Brasil

  26. WilliamSnado表示:

    aviator game: aviator – aviator bet

  27. WilliamSnado表示:

    aviator betano: aviator bet – aviator jogar

  28. HowardPlomy表示:

    play aviator: aviator betting game – play aviator

  29. Thomasrib表示:

    aviator malawi: aviator malawi – aviator bet malawi

  30. Thomasrib表示:

    aviator malawi: aviator malawi – aviator game online

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。