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

  1. JimmieTup表示:

    https://lisinoprilus.com/# cheap lisinopril 40 mg

  2. Jaimevoink表示:

    how to get cheap clomid price where buy generic clomid where buy cheap clomid without prescription

  3. JimmieTup表示:

    http://azithromycinus.com/# can i buy zithromax over the counter in canada

  4. After study a handful of the blog articles on your own website now, i genuinely as if your method of blogging. I bookmarked it to my bookmark site list and will be checking back soon. Pls take a look at my web site at the same time and tell me what you think.

  5. JimmieTup表示:

    https://clomid.store/# generic clomid without insurance

  6. ArmandoDat表示:

    п»їcytotec pills online: buy cytotec in usa – cytotec buy online usa

  7. He later presumably survived the nice Jedi Purge.

  8. Jaimevoink表示:

    can i purchase clomid without prescription how to buy generic clomid no prescription where to buy generic clomid without rx

  9. Jaimevoink表示:

    how to buy clomid without prescription buying cheap clomid online buying clomid pills

  10. JimmieTup表示:

    https://azithromycinus.com/# where can i get zithromax

  11. When a blind man bears the standard pity those who follow…. Where ignorance is bliss ‘tis folly to be wise….

  12. ArmandoDat表示:

    ciprofloxacin: ciprofloxacin order online – where to buy cipro online

  13. ArmandoDat表示:

    buy cipro online usa: cipro online no prescription in the usa – buy cipro online without prescription

  14. I will right away clutch your rss as I can’t find your email subscription link or e-newsletter service. Do you have any? Please let me realize so that I could subscribe. Thanks.

  15. ArmandoDat表示:

    zestril 5 mg india: lisinopril 90 pills cost – lisinopril for sale online

  16. Jaimevoink表示:

    cytotec buy online usa Abortion pills online cytotec abortion pill

  17. ArmandoDat表示:

    can i order cheap clomid tablets: how can i get generic clomid without insurance – cheap clomid for sale

  18. Jaimevoink表示:

    can i order clomid pill [url=http://clomid.store/#]how to buy generic clomid[/url] can i buy generic clomid pills

  19. go88表示:

    Introducing to you the most prestigious online entertainment address today. Visit now to experience now!

  20. Hello! I simply wish to make a enormous thumbs up with the great info you’ve got here for this post. We are coming back to your site to get more detailed soon.

  21. JimmieTup表示:

    http://clomid.store/# order generic clomid prices

  22. Ccyria表示:

    buy crixivan without prescription – buy cheap emulgel purchase cheap diclofenac gel

  23. plumbing表示:

    You made some clear points there. I did a search on the topic and found most people will go along with with your blog.

  24. JimmieTup表示:

    http://cytotec.top/# buy cytotec online

  25. JimmieTup表示:

    https://clomid.store/# how to get clomid without a prescription

發佈留言

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