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

49,454 Responses

  1. Marvinwat表示:

    блекспрут bs2tor nl – блекспрут вход, blacksprut onion зеркало

  2. LincolnFeamp表示:

    breakingbad – cyclohexylamine synthesis, breaking bad forum

  3. LincolnFeamp表示:

    Speciality Chemicals – breaking bad forum, bbgate forum

  4. Wallacefex表示:

    sweet bonanza guncel: sweet bonanza yorumlar – sweet bonanza guncel

  5. Jimmylop表示:

    kraken перевод – kraken актуальное, кракен тор

  6. JasonUnsoG表示:

    http://casinositeleri25.com/# Casino Siteleri

  7. BradleyEdino表示:

    casinox en guvenilir casino siteleri en iyi yasal bahis siteleri

  8. Darth Atrius – Historical Darkish Lord of the Sith who lived before Darth Bane and the trendy Sith.

  9. BradleyEdino表示:

    Deneme Bonusu Veren Siteler Deneme Bonusu Veren Siteler en guvenilir casino siteleri

  10. JasonUnsoG表示:

    https://slotsiteleri25.com/# az parayla cok kazandiran slot oyunlar?

  11. BradleyEdino表示:

    sweet bonanza oyna sweet bonanza oyna sweet bonanza guncel

  12. Food co-ops are extra like conventional grocery stores than farmers’ markets.

  13. By 9:17 a.m., all New York City airports are shut down.

  14. Word: Since Carlsen reached 7½ factors with sport 11, the match ended despite the fact that not all 14 games have been performed.

  15. Marvinwat表示:

    как зайти через blacksprut – рабочая blacksprut, блекспрут bs2tor nl

  16. Heavy rods in tracks prevent opening of sliding glass doorways absolutely.

  17. You should always work very closely with your accountant when negotiating a deal with a outside funding source especially if it involves the purchase of real estate, machinery, equipment, or related assets.

  18. Taken together, these two notions are apt to illuminate the virtues and vices of that relatively quixotic ‘philosophy’ which matches by the identify of determinism.

  19. Wallacefex表示:

    guvenilir casino siteleri: Canl? Casino Siteleri – canl? casino siteleri

  20. Pvlvdf表示:

    buy accutane 40mg pills – linezolid 600 mg us buy zyvox 600 mg pill

  21. LincolnFeamp表示:

    breakingbad – Breaking Bad chemistry, breakingbad forum

  22. This year’s edition, which ran from June 7-10, pumped up the visible pleasure even greater than usual with a massive off-stage lighting show that wrapped across the phases and surrounded the viewers in a sea of fixing colours.

  23. Mandelbrot observed that giant movements in costs (i.e.

  24. Jimmylop表示:

    https kraken20 at chat – kraken актуальные ссылки, www kraken

  25. Deputy Chairman, Australian Dried Fruits Management Board.

  26. Jimmylop表示:

    kra13.at – как зайти на кракен, kraken tor ссылка bazaonion vip

  27. наркология вывод из запоя ростов http://www.vyvod-iz-zapoya-rostov229.ru/ .

  28. JasonUnsoG表示:

    https://sweetbonanza25.com/# sweet bonanza oyna

發佈留言

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