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

  1. RobertRix表示:

    http://prednisoneall.shop/# prednisone 10mg buy online

  2. DavidrAilk表示:

    where can i purchase zithromax online how to get zithromax zithromax capsules

  3. Edwardjem表示:

    cost clomid without insurance: order cheap clomid pills – can i buy generic clomid without rx

  4. DavidRuils表示:

    can you buy clomid without dr prescription: can i buy clomid – can i purchase clomid without prescription

  5. DavidrAilk表示:

    zithromax purchase online zithromax over the counter canada buy zithromax 1000mg online

  6. RobertRix表示:

    https://zithromaxall.com/# can you buy zithromax over the counter in canada

  7. Wjcson表示:

    order augmentin 625mg online cheap – cipro 1000mg for sale baycip pills

  8. RobertRix表示:

    http://prednisoneall.com/# 50mg prednisone tablet

  9. DavidRuils表示:

    order amoxicillin online: order amoxicillin uk – amoxicillin 500 mg without a prescription

  10. DavidrAilk表示:

    amoxicillin 500 capsule where to buy amoxicillin pharmacy amoxicillin discount coupon

  11. DavidrAilk表示:

    can i purchase prednisone without a prescription prednisone otc uk buy prednisone online canada

  12. RobertRix表示:

    https://amoxilall.com/# buy amoxicillin online mexico

  13. RobertRix表示:

    http://prednisoneall.shop/# prednisone 10mg prices

  14. DavidrAilk表示:

    average cost of generic zithromax buy cheap generic zithromax zithromax generic price

  15. JamesGonRy表示:

    https://prednisoneall.shop/# prednisone daily

  16. DavidRuils表示:

    buying cheap clomid without dr prescription: where can i buy clomid without insurance – how can i get clomid prices

  17. Edwardjem表示:

    apo prednisone: prednisone 20mg prescription cost – purchase prednisone

  18. JamesGonRy表示:

    https://prednisoneall.com/# prednisone generic cost

  19. RobertRix表示:

    http://amoxilall.shop/# amoxicillin 500 mg capsule

  20. RobertRix表示:

    https://zithromaxall.com/# zithromax z-pak price without insurance

  21. Edwardjem表示:

    where can i buy amoxicillin over the counter: amoxil generic – amoxicillin 500 tablet

  22. DavidrAilk表示:

    prednisone pills 10 mg prednisone without prescription prednisone 2.5 mg daily

  23. Shaneevige表示:

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

    Официальный сайт Гама казино

  24. DavidrAilk表示:

    can i buy generic clomid online how to buy cheap clomid no prescription can i order clomid online

  25. Edwardjem表示:

    amoxicillin tablets in india: amoxicillin azithromycin – amoxicillin 500mg

  26. DavidrAilk表示:

    cheap prednisone 20 mg 5 prednisone in mexico purchase prednisone 10mg

  27. RobertRix表示:

    http://amoxilall.com/# generic amoxicillin over the counter

  28. DavidRuils表示:

    prednisone generic cost: can i buy prednisone online without a prescription – prednisone online paypal

  29. RobertRix表示:

    http://prednisoneall.com/# prednisone for sale

  30. Danielfes表示:

    I was able to find good information from your articles.

    https://pokhara.kr/bbs/board.php?bo_table=free&wr_id=379285

發佈留言

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