透過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...

46,551 Responses

  1. Josephcoume表示:

    nolvadex d tamoxifen bone pain

  2. Kjvmbq表示:

    generic metoprolol 50mg medrol 16 mg otc levitra 20mg generic

  3. Andreprark表示:

    https://tamoxifen.best/# tamoxifen and osteoporosis

  4. preerse表示:

    Based on our previous data 8 , we expected a cumulative pregnancy rate of 0. success rate of clomid

  5. This is an excellent blog post, thank you for sharing it! Check out my website

  6. MatthewSycle表示:

    https://molnupiravir.life/# molnupiravir vietnam

  7. elalley表示:

    albuterol sulfate inhalation $15 ventolin albuterol inhaler price cvs albuterol sulfate inhaler

  8. MatthewSycle表示:

    https://viagracanada.xyz/# herbal substitutes for viagra

  9. Adoreepisn表示:

    live gay young male sex chat rooms
    gay hot chat
    live free gay web cam chat rooms

  10. Dennisscalp表示:

    https://paxlovid.best/# paxlovid nebenwirkungen

  11. preerse表示:

    clomid for pregnancy To make matters worse, anabolic steroids are also notorious for prompting hair loss, much more so than Nolvadex, so just imagine what s going to happen when you take the two together.

  12. Josephcoume表示:

    п»їdcis tamoxifen nolvadex for sale amazon

  13. Noxescods表示:

    I recommend doing a min of 25mgED from the very first day. clomid side effects in women Clomid just didn t work for me, but I wasn t on metformin at the time I was using it.

  14. Noxescods表示:

    We ve just relocated to Paris for Nick s job. clomid side effects I am 59 years old with PSA of 5.

  15. Skrrjh表示:

    generic doxycycline buy doxycycline 200mg pill ivermectin 6mg without a doctor prescription

  16. YoBit.net表示:

    Helpful article! YoBit.net

  17. Marilinpisn表示:

    gay video cam chat
    top gay chat rooms
    omegle gay chat

  18. Bgikoc表示:

    purchase doxycycline without prescription olumiant online order ivermectin coronavirus

  19. ShooraBor表示:

    I start letro folowing these instructions forums. clomid for sale

  20. MatthewSycle表示:

    https://withoutdoctorprescription.xyz/# non prescription ed drugs

  21. ThekPhend表示:

    2 than in the regularly menstruating group 48. buy clomiphene citrate Their goal was to study 2,000 people, but they ended up stopping the study early because folic acid supplementation had such a large positive effect they thought it was only right to give it to all participants.

  22. Noxescods表示:

    The greed of the skeleton patriarch made him doomed to fall into type 2 diabetes and liver damage a trap blood sugar 210 after meal that Zhao Ling arranged for him. where to buy clomid trusted site I track everything and my cycle is so regular.

  23. Bmsqik表示:

    doxycycline 100mg tablet buy olumiant generic stromectol xl

  24. Marilinpisn表示:

    teen gay dick snap chat
    boys gay chat
    gay chat site

  25. Josephcoume表示:

    molnupiravir supplier a pill to treat covid

發佈留言

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