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

48,184 Responses

  1. Timothyket表示:

    non prescription prednisone 20mg: prednisone 10 mg – prednisone 30 mg tablet

  2. Timothyket表示:

    10 mg prednisone: how to buy prednisone – prednisone for sale online

  3. Michaeltaisa表示:

    https://amoxil.icu/# cost of amoxicillin prescription

  4. Michaeltaisa表示:

    http://amoxil.icu/# amoxicillin 500mg price in canada

  5. Timothyket表示:

    compare prednisone prices: prednisone without prescription.net – prednisone for cheap

  6. Michaeltaisa表示:

    http://amoxil.icu/# amoxicillin 500 mg cost

  7. MichaelSom表示:

    get clomid now: order generic clomid no prescription – cost cheap clomid online

  8. Antoniobus表示:

    http://ciprofloxacin.life/# ciprofloxacin 500 mg tablet price

  9. Timothyket表示:

    prednisone capsules: can you buy prednisone over the counter uk – prednisone 10 mg

  10. MichaelSom表示:

    order cheap clomid no prescription: can i purchase generic clomid tablets – where to buy generic clomid without insurance

  11. sitemap表示:

    Heyya terrificc website! Doess runing a log similar too thjs takke
    a massive amount work? I’ve absolutly noo understanding of
    coding howewver I waas hopig to start my ownn blog in thee near future.
    Anyway, shold yoou have aany ideas or tips forr nnew blog
    owners pleaae share. I knw thius iss off subject howevsr I simjply wanted too
    ask. Many thanks!

  12. MichaelSom表示:

    where can i buy generic clomid without prescription: where can i buy generic clomid without insurance – can i buy clomid without prescription

  13. Antoniobus表示:

    https://ciprofloxacin.life/# cipro ciprofloxacin

  14. MichaelSom表示:

    where to buy clomid no prescription: where can i get clomid now – how can i get generic clomid price

  15. MichaelSom表示:

    can i order clomid: where to get clomid without rx – can i purchase cheap clomid

  16. Antoniobus表示:

    http://ciprofloxacin.life/# ciprofloxacin 500 mg tablet price

  17. Antoniobus表示:

    http://ciprofloxacin.life/# buy generic ciprofloxacin

  18. Michaeltaisa表示:

    https://amoxil.icu/# cost of amoxicillin prescription

  19. MichaelSom表示:

    cost cheap clomid pills: how to buy clomid without rx – clomid rx

  20. Michaeltaisa表示:

    http://amoxil.icu/# amoxil pharmacy

  21. MichaelSom表示:

    buy cheap clomid no prescription: where to buy generic clomid no prescription – where can i buy clomid

  22. Antoniobus表示:

    http://ciprofloxacin.life/# cipro 500mg best prices

  23. Antoniobus表示:

    https://ciprofloxacin.life/# buy cipro cheap

  24. MichaelSom表示:

    can you get generic clomid pills: can you buy generic clomid no prescription – how to get generic clomid without insurance

  25. MichaelSom表示:

    how to buy clomid no prescription: cost generic clomid pills – how can i get generic clomid online

  26. Antoniobus表示:

    https://ciprofloxacin.life/# ciprofloxacin order online

  27. Michaeltaisa表示:

    http://amoxil.icu/# amoxicillin without a prescription

  28. Antoniobus表示:

    http://ciprofloxacin.life/# ciprofloxacin generic

  29. Timothyket表示:

    prednisone 200 mg tablets: order prednisone online canada – prednisone 54899

  30. Antoniobus表示:

    https://ciprofloxacin.life/# ciprofloxacin generic price

發佈留言

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