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

  1. Jaimevoink表示:

    Cytotec 200mcg price cytotec abortion pill Misoprostol 200 mg buy online

  2. You lost me, friend. I am talking about, I imagine I purchase what youre saying. I’m sure what you’re saying, nevertheless, you just appear to have forgotten that you will find a few other folks from the world who view this trouble for it is really and could perhaps not agree with you. You could be turning away many people that was lovers of the website.

  3. Jaimevoink表示:

    cipro online no prescription in the usa cipro ciprofloxacin generic price

  4. RobertUnmab表示:

    cytotec pills buy online: buy cytotec over the counter – п»їcytotec pills online

  5. ArmandoDat表示:

    zithromax for sale usa: generic zithromax online paypal – zithromax order online uk

  6. ArmandoDat表示:

    cost cheap clomid without dr prescription: can you get cheap clomid no prescription – order generic clomid

  7. Jaimevoink表示:

    zithromax for sale us zithromax 250 mg pill zithromax online paypal

  8. JimmieTup表示:

    https://clomid.store/# clomid pills

  9. Jaimevoink表示:

    Misoprostol 200 mg buy online buy cytotec pills Cytotec 200mcg price

  10. RobertUnmab表示:

    where buy clomid without insurance: where to buy cheap clomid tablets – how to get cheap clomid

  11. ArmandoDat表示:

    buy cipro no rx: where can i buy cipro online – buy cipro online canada

  12. RobertUnmab表示:

    where to buy cheap clomid online: cost of generic clomid pills – where can i get generic clomid no prescription

  13. RobertUnmab表示:

    zithromax prescription in canada: can you buy zithromax over the counter in canada – zithromax over the counter

  14. Jaimevoink表示:

    how to get clomid no prescription clomid without insurance buying clomid prices

  15. ArmandoDat表示:

    zithromax for sale 500 mg: where can you buy zithromax – zithromax without prescription

  16. ArmandoDat表示:

    lisinopril price in canada: lisinopril 20 mg price online – can you buy lisinopril over the counter

  17. JimmieTup表示:

    http://ciprofloxacin.cheap/# buy generic ciprofloxacin

  18. RobertUnmab表示:

    lisinopril brand name canada: lisinopril 20mg buy – lisinopril 102

  19. JimmieTup表示:

    https://azithromycinus.com/# where can i buy zithromax capsules

  20. JimmieTup表示:

    http://azithromycinus.com/# buy cheap zithromax online

  21. ArmandoDat表示:

    Misoprostol 200 mg buy online: Abortion pills online – Abortion pills online

  22. ArmandoDat表示:

    zithromax 500 tablet: generic zithromax medicine – buy generic zithromax online

  23. JimmieTup表示:

    https://lisinoprilus.com/# lisinopril 30 mg price

  24. Jaimevoink表示:

    buy cytotec online buy cytotec pills buy cytotec

  25. Jaimevoink表示:

    prinivil cost lisinopril 5 mg india price lisinopril 80 mg

  26. RobertUnmab表示:

    where buy generic clomid for sale: where to buy clomid online – get generic clomid no prescription

  27. Hello! I just now would like to supply a enormous thumbs up for any fantastic information you’ve got here within this post. I am coming back to your blog site for more soon.

發佈留言

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