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

  1. Danielnek表示:

    http://amoxil.icu/# over the counter amoxicillin canada

  2. MichaelWence表示:

    order clomid without a prescription can i purchase generic clomid pill – cost of generic clomid no prescription

  3. Antoniobus表示:

    https://ciprofloxacin.life/# purchase cipro

  4. Antoniobus表示:

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

  5. Michaeltaisa表示:

    http://amoxil.icu/# amoxicillin discount coupon

  6. MichaelWence表示:

    where buy clomid price can you buy clomid without insurance – how to get cheap clomid without insurance

  7. MichaelWence表示:

    get generic clomid without rx cost clomid without a prescription – buying cheap clomid without dr prescription

  8. Danielnek表示:

    https://amoxil.icu/# amoxicillin 875 mg tablet

  9. Danielnek表示:

    https://amoxil.icu/# price for amoxicillin 875 mg

  10. MichaelSom表示:

    buying clomid tablets: generic clomid without a prescription – where to buy generic clomid without rx

  11. Danielnek表示:

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

  12. Timothyket表示:

    buy prednisone without a prescription: prednisone canada pharmacy – cheap generic prednisone

  13. Timothyket表示:

    prednisone for sale: average cost of prednisone 20 mg – prednisone 20mg for sale

  14. MichaelSom表示:

    cost cheap clomid price: where to buy generic clomid without prescription – can i order cheap clomid without a prescription

  15. Danielnek表示:

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

  16. Danielnek表示:

    https://amoxil.icu/# price of amoxicillin without insurance

  17. Отличный сайт, всем рекомендую заглянуть сюда!киного

  18. Отличный сайт, всем рекомендую заглянуть сюда!kinogo

  19. Oiseta表示:

    names of prescription allergy pills otc allergy medication comparison chart best allergy medicine for rash

  20. Отличный сайт, всем рекомендую заглянуть сюда!kinogo.vc

  21. Я рекомендую всем лучшее оригинальное вавада казино.

  22. WillianSeEmO表示:

    canadian pharmacy amoxicillin purchase amoxicillin online without prescription generic for amoxicillin

  23. AndrewRex表示:

    amoxicillin without prescription: where to buy amoxicillin 500mg – can i buy amoxicillin over the counter

  24. JasonWaync表示:

    ciprofloxacin generic price: ciprofloxacin order online – cipro online no prescription in the usa

  25. Stephenbum表示:

    http://clomid.site/# can i get generic clomid prices

  26. Здесь определенно лучшая порошковая краска. Рекомендую!

  27. GeorgeEteli表示:

    amoxicillin online without prescription: amoxicillin buy no prescription – amoxicillin discount coupon

  28. Hermanges表示:

    prednisone 54899 prednisone 40 mg prednisone 7.5 mg

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

  30. Raymondsmero表示:

    https://paxlovid.win/# buy paxlovid online

發佈留言

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