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

  1. DavidrAilk表示:

    clomid without a prescription get generic clomid pills can i buy clomid no prescription

  2. DavidrAilk表示:

    cost of amoxicillin prescription rexall pharmacy amoxicillin 500mg amoxil generic

  3. RobertRix表示:

    https://amoxilall.shop/# how to buy amoxicillin online

  4. RobertRix表示:

    https://clomidall.shop/# how to get cheap clomid without insurance

  5. DavidRuils表示:

    prednisone in mexico: prednisone 500 mg tablet – prednisone uk

  6. JamesGonRy表示:

    https://zithromaxall.shop/# zithromax price south africa

  7. DavidrAilk表示:

    prednisone purchase canada prednisone 50 mg tablet cost generic prednisone cost

  8. JamesGonRy表示:

    http://prednisoneall.com/# where to buy prednisone in australia

  9. RobertRix表示:

    https://amoxilall.com/# buy amoxicillin over the counter uk

  10. RobertRix表示:

    https://zithromaxall.com/# zithromax antibiotic

  11. DavidrAilk表示:

    how to get generic clomid pill can i get cheap clomid now get generic clomid without rx

  12. DavidrAilk表示:

    prednisone buy no prescription prednisone non prescription prednisone 20mg

  13. Edwardjem表示:

    how to get prednisone without a prescription: how can i order prednisone – how can i get prednisone

  14. DavidRuils表示:

    zithromax tablets: where can i get zithromax over the counter – zithromax cost uk

  15. DavidrAilk表示:

    where can i get clomid prices how to buy clomid for sale can you buy cheap clomid without dr prescription

  16. Edwardjem表示:

    6 prednisone: online prednisone 5mg – prednisone pack

  17. RobertRix表示:

    http://zithromaxall.shop/# where can i buy zithromax capsules

  18. JamesGonRy表示:

    https://prednisoneall.com/# online prednisone 5mg

  19. RobertRix表示:

    http://clomidall.com/# can i purchase clomid no prescription

  20. JamesGonRy表示:

    https://prednisoneall.shop/# buy prednisone canada

  21. Edwardjem表示:

    cost of generic zithromax: zithromax without prescription – zithromax buy online

  22. DavidrAilk表示:

    cost generic clomid pills can you get clomid without dr prescription clomid

  23. DavidrAilk表示:

    where can i get cheap clomid without a prescription get clomid no prescription order generic clomid without rx

  24. RobertRix表示:

    https://zithromaxall.com/# zithromax 500

  25. RobertRix表示:

    http://zithromaxall.shop/# how to buy zithromax online

  26. DavidrAilk表示:

    zithromax 500 price zithromax for sale 500 mg zithromax online

  27. Edwardjem表示:

    prednisone for cheap: prednisone brand name canada – prednisone 5mg capsules

  28. DavidRuils表示:

    order amoxicillin no prescription: amoxicillin online pharmacy – amoxicillin 500 mg where to buy

  29. RobertRix表示:

    http://clomidall.com/# buying generic clomid without prescription

  30. RobertRix表示:

    http://zithromaxall.com/# buy zithromax no prescription

發佈留言

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