透過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...

47,860 Responses

  1. Ronniewessy表示:

    http://farmacia.best/# farmacia online madrid

  2. Mariophity表示:

    п»їViagra online cerca de Madrid: comprar viagra en espana – viagra precio 2022

  3. Ronniewessy表示:

    https://tadalafilo.pro/# farmacia online 24 horas

  4. Ronniewessy表示:

    http://kamagraes.site/# farmacia envíos internacionales

  5. JasonBib表示:

    http://farmacia.best/# farmacia online madrid

  6. Eddieduart表示:

    farmacia online madrid Cialis sin receta farmacia barata

  7. Ronniewessy表示:

    https://vardenafilo.icu/# farmacia online madrid

  8. Mariophity表示:

    farmacias online seguras: Levitra sin receta – farmacia online envГ­o gratis

  9. Ronniewessy表示:

    http://kamagraes.site/# farmacias online seguras

  10. Ronniewessy表示:

    https://farmacia.best/# farmacias online seguras en españa

  11. JasonBib表示:

    https://farmacia.best/# п»їfarmacia online

  12. Ronniewessy表示:

    https://kamagraes.site/# farmacia 24h

  13. Eddieduart表示:

    viagra para hombre precio farmacias similares comprar viagra en espana viagra 100 mg precio en farmacias

  14. Tracyseils表示:

    miglior sito per comprare viagra online: viagra prezzo – kamagra senza ricetta in farmacia

  15. Tracyseils表示:

    migliori farmacie online 2023: avanafil generico prezzo – top farmacia online

  16. Sidneycrurn表示:

    http://sildenafilit.bid/# viagra generico recensioni

  17. Sonnyaftem表示:

    farmaci senza ricetta elenco: Tadalafil prezzo – farmacia online

  18. MiliiaLulubiz表示:

    Recommend viewing this sitesheepskin coats

  19. Tracyseils表示:

    farmacia online più conveniente: farmacia online – farmacia online più conveniente

  20. TerryRam表示:

    farmacia online senza ricetta avanafil farmacia online piГ№ conveniente

  21. Tracyseils表示:

    farmacia online migliore: Dove acquistare Cialis online sicuro – farmacie on line spedizione gratuita

  22. sitemap表示:

    Its like youu read my mind! Youu apprar to know a lot about this, like you wroote the boook in it or something.
    I think thyat youu coul do with a ffew pkcs too driive thhe messagee hone a bit, buut innstead of that,
    this is great blog. A fantastoc read. I will certrainly be back.

  23. Tracyseils表示:

    comprare farmaci online all’estero: farmacia online migliore – acquisto farmaci con ricetta

  24. Tracyseils表示:

    farmacia online migliore: comprare avanafil senza ricetta – farmacie online sicure

  25. Tracyseils表示:

    top farmacia online: farmacia online più conveniente – comprare farmaci online all’estero

  26. TerryRam表示:

    cialis farmacia senza ricetta viagra generico viagra originale recensioni

  27. Tracyseils表示:

    cialis farmacia senza ricetta: sildenafil prezzo – miglior sito dove acquistare viagra

  28. Sidneycrurn表示:

    http://sildenafilit.bid/# pillole per erezione in farmacia senza ricetta

  29. Sonnyaftem表示:

    viagra originale recensioni: viagra generico – viagra online spedizione gratuita

  30. Tracyseils表示:

    farmacie on line spedizione gratuita: Tadalafil generico – farmacia online miglior prezzo

發佈留言

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