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

  1. Hermanges表示:

    paxlovid generic paxlovid india paxlovid price

  2. JasonWal表示:

    amoxicillin 775 mg: amoxicillin in india – amoxicillin in india

  3. Отличный сайт, всем рекомендую заглянуть сюда!Тут Вы можете приобрестиЧехол для телефона

  4. Рекомендую здесь всем покупать чехол на телефон самсунг.

  5. JasonWal表示:

    can i order generic clomid without rx: where can i get cheap clomid without prescription – where can i get generic clomid pills

  6. BrianDutle表示:

    https://clomid.site/# how to buy clomid no prescription

  7. JasonWal表示:

    paxlovid cost without insurance: paxlovid price – paxlovid for sale

  8. Hermanges表示:

    where can i buy cipro online cipro cipro 500mg best prices

  9. JasonWal表示:

    ciprofloxacin over the counter: buy generic ciprofloxacin – buy cipro online canada

  10. Raymondsmero表示:

    https://ciprofloxacin.life/# ciprofloxacin 500mg buy online

  11. JasonWal表示:

    amoxicillin online purchase: where can i buy amoxicillin over the counter – canadian pharmacy amoxicillin

  12. JasonWal表示:

    buy ciprofloxacin over the counter: ciprofloxacin 500 mg tablet price – cipro

  13. JasonWal表示:

    buy cipro: cipro pharmacy – buy cipro

  14. Всем рекомендую классное Раменбет казино.

  15. JasonWal表示:

    buy paxlovid online: paxlovid – paxlovid pharmacy

  16. Hermanges表示:

    paxlovid buy paxlovid buy paxlovid cost without insurance

  17. JasonWal表示:

    order cheap clomid now: cost of clomid for sale – where to buy generic clomid pills

  18. Отличный сайт, всем рекомендую заглянуть сюда!Тут Вы можете приобрестиЧехол на телефон

  19. JasonWal表示:

    can i order cheap clomid no prescription: where can i buy cheap clomid without rx – can i get generic clomid without prescription

  20. Raymondsmero表示:

    http://clomid.site/# where to buy clomid without prescription

  21. JasonWal表示:

    buy ciprofloxacin over the counter: buy cipro online – buy cipro cheap

  22. BrianDutle表示:

    https://prednisone.bid/# over the counter prednisone pills

  23. JasonWal表示:

    can we buy amoxcillin 500mg on ebay without prescription: price for amoxicillin 875 mg – buy cheap amoxicillin online

  24. JasonWal表示:

    cost of generic clomid: how to get generic clomid without a prescription – how to get cheap clomid for sale

  25. Hermanges表示:

    paxlovid pharmacy paxlovid pharmacy paxlovid buy

  26. JasonWal表示:

    can i buy generic clomid online: buy cheap clomid prices – how can i get generic clomid price

  27. JasonWal表示:

    can i buy cheap clomid price: can you buy cheap clomid without insurance – buy generic clomid prices

  28. Raymondsmero表示:

    http://paxlovid.win/# paxlovid price

  29. JasonWal表示:

    prednisone 200 mg tablets: prednisone 10 mg online – prednisone tabs 20 mg

  30. JasonWal表示:

    paxlovid buy: Paxlovid over the counter – Paxlovid buy online

發佈留言

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