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

  1. Michaelhig表示:

    zithromax canadian pharmacy: zithromax buy – can you buy zithromax over the counter in mexico

  2. Michaelhig表示:

    ivermectin virus: ivermectin lotion 0.5 – cost of ivermectin 1% cream

  3. BryanAgist表示:

    zithromax 250 mg pill: buy zithromax 1000 mg online – zithromax 500mg over the counter

  4. BrainDer表示:

    https://prednisonea.store/# prednisone pharmacy

  5. BryanAgist表示:

    ivermectin 8000 mcg: stromectol pills – ivermectin 5 mg price

  6. JosephBix表示:

    http://stromectola.top/# stromectol south africa

  7. BrainDer表示:

    https://amoxicillina.top/# generic for amoxicillin

  8. Michaelhig表示:

    can i get clomid pill: can i purchase generic clomid pill – buy cheap clomid no prescription

  9. BrainDer表示:

    https://amoxicillina.top/# amoxicillin online purchase

  10. Michaelhig表示:

    ivermectin tablets: stromectol buy – ivermectin 250ml

  11. Jameswaw表示:

    buy zithromax online australia where can i get zithromax over the counter zithromax 500 mg

  12. Jameswaw表示:

    clomid brand name cost of cheap clomid tablets where to buy cheap clomid without rx

  13. Mlvzwz表示:

    medrol 4mg without a doctor prescription – medrol 4 mg without prescription astelin cheap

  14. BrainDer表示:

    https://amoxicillina.top/# can i buy amoxicillin over the counter in australia

  15. Ismaelfer表示:

    Приветики!
    Приобретите диплом института или колледжа с гарантией качества и доставкой по России без предоплаты.
    http://saksx-attestats.ru/
    Хотите купить диплом Вуза недорого и получить его почтой без предоплаты? Мы можем помочь вам сделать это.
    Закажите диплом ВУЗа с доставкой по России без предоплаты и с возможностью оплаты после получения – надежно, выгодно, безопасно!

  16. Отличный сайт! Всем рекомендую! заверение электронной переписки

  17. Michaelhig表示:

    where can i buy zithromax capsules: zithromax pill – how to get zithromax online

  18. JosephBix表示:

    https://azithromycina.pro/# zithromax generic cost

  19. Jameswaw表示:

    over the counter amoxicillin canada amoxicillin tablet 500mg how to get amoxicillin

  20. DomenicEcoff表示:

    zithromax 500mg over the counter: buy zithromax online – can you buy zithromax over the counter in canada

  21. BrainDer表示:

    http://prednisonea.store/# prednisone pill prices

  22. Michaelhig表示:

    amoxicillin generic: amoxicillin cost australia – amoxicillin order online

  23. BrainDer表示:

    https://stromectola.top/# stromectol 3 mg tablet

  24. DomenicEcoff表示:

    stromectol prices: minocycline online – ivermectin cost australia

  25. BryanAgist表示:

    ivermectin 50ml: stromectol canada – ivermectin buy australia

  26. BryanAgist表示:

    ordering prednisone: prednisone 10 – prednisone 21 pack

  27. Jameswaw表示:

    zithromax 500mg price cheap zithromax pills zithromax order online uk

  28. DomenicEcoff表示:

    over the counter amoxicillin: amoxicillin 500mg capsule cost – can you buy amoxicillin over the counter

  29. Michaelhig表示:

    prednisone 20mg prices: cheapest prednisone no prescription – prednisone brand name canada

  30. Michaelhig表示:

    buy amoxicillin 500mg: where can i buy amoxicillin over the counter – can you buy amoxicillin over the counter in canada

發佈留言

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