透過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,118 Responses

  1. Ronaldpsymn表示:

    https://finasteride.store/# order propecia no prescription

  2. RandallTaulp表示:

    hysterectomy after breast cancer tamoxifen: tamoxifen cancer – tamoxifen hormone therapy

  3. Ronaldpsymn表示:

    http://nolvadex.life/# effexor and tamoxifen

  4. WilliamRhype表示:

    http://lisinopril.network/# buy lisinopril

  5. Ronaldpsymn表示:

    https://nolvadex.life/# nolvadex only pct

  6. WilliamRhype表示:

    https://ciprofloxacin.tech/# buy cipro online without prescription

  7. Рекомендую надёжную Букмекерскую контору 1win

  8. 1win499unern表示:

    Рекомендую Лучшую Букмекерскую контору 1win

  9. Davidtes表示:

    zestril 5mg price in india lisinopril online without a prescription lisinopril 20 mg over the counter

  10. RandallTaulp表示:

    buy cipro online: buy cipro online without prescription – cipro pharmacy

  11. Davidtes表示:

    cost generic propecia for sale buying propecia without a prescription get propecia

  12. Ronaldpsymn表示:

    https://ciprofloxacin.tech/# cipro ciprofloxacin

  13. RandallTaulp表示:

    cost propecia tablets: order cheap propecia without insurance – cost of generic propecia price

  14. WilliamRhype表示:

    http://lisinopril.network/# lisinopril 40 mg india

  15. Davidtes表示:

    buy ciprofloxacin over the counter antibiotics cipro cipro ciprofloxacin

  16. Davidtes表示:

    cipro for sale buy ciprofloxacin over the counter buy cipro cheap

  17. WilliamRhype表示:

    http://nolvadex.life/# does tamoxifen cause bone loss

  18. RandallTaulp表示:

    nolvadex during cycle: natural alternatives to tamoxifen – tamoxifen brand name

  19. RandallTaulp表示:

    zestril 5mg price in india: lisinopril 10 12.5 mg – cost of brand name lisinopril

  20. Рекомендую топовую Букмекерскую контору 1win

  21. Frfgerk40dag表示:

    Recomendo o melhor site Blaze

  22. WilliamRhype表示:

    http://finasteride.store/# order cheap propecia without prescription

  23. WilliamRhype表示:

    http://nolvadex.life/# should i take tamoxifen

  24. Davidtes表示:

    can you buy lisinopril cheapest price for lisinopril ordering lisinopril without a prescription

  25. O melhor cassino, eu o recomendo – Blaze

  26. Frfgerk42dag表示:

    O melhor cassino do mundo Blaze

  27. Davidtes表示:

    tamoxifen for sale does tamoxifen make you tired tamoxifen cyp2d6

  28. RandallTaulp表示:

    how much is 30 lisinopril: lisinopril medication generic – buy lisinopril 20 mg online canada

  29. Davidtes表示:

    lisinopril tabs prinivil brand name zestril 10 mg price

發佈留言

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