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

  1. DavidRak表示:

    ivermectin pill cost ivermectin cost stromectol 3mg tablets

  2. Stephenpem表示:

    http://amoxil.cheap/# amoxicillin discount coupon

  3. Charlesdix表示:

    https://furosemide.guru/# lasix pills

  4. Charlesdix表示:

    https://amoxil.cheap/# order amoxicillin 500mg

  5. JamesCof表示:

    lasix pills: Over The Counter Lasix – lasix 40mg

  6. DavidRak表示:

    lasix generic name Buy Furosemide lasix generic name

  7. LeonardFaulk表示:

    prednisolone prednisone: prednisone 40 mg tablet – cost of prednisone 40 mg

  8. JamesCof表示:

    lisinopril 200 mg: prinivil 20 mg cost – cost for 2 mg lisinopril

  9. Charlesdix表示:

    https://buyprednisone.store/# cost of prednisone in canada

  10. JamesCof表示:

    buy amoxicillin 500mg online: where to buy amoxicillin pharmacy – amoxicillin tablets in india

  11. Charlesdix表示:

    http://buyprednisone.store/# buy prednisone nz

  12. Stephenpem表示:

    http://lisinopril.top/# drug lisinopril 5 mg

  13. JamesCof表示:

    prednisone tablets india: 80 mg prednisone daily – prednisone uk buy

  14. LeonardFaulk表示:

    stromectol pill: buy ivermectin cream for humans – stromectol covid 19

  15. JamesCof表示:

    buy prinivil: zestoretic generic – lisinopril 1 mg tablet

  16. DavidRak表示:

    topical ivermectin cost stromectol 3mg stromectol over the counter

  17. Charlesdix表示:

    http://buyprednisone.store/# order prednisone 10 mg tablet

  18. Charlesdix表示:

    http://buyprednisone.store/# how much is prednisone 10mg

  19. JamesCof表示:

    where can i buy stromectol: ivermectin 9 mg tablet – stromectol coronavirus

  20. LeonardFaulk表示:

    amoxicillin discount: where can i get amoxicillin – amoxicillin 500mg price in canada

  21. JamesCof表示:

    can i buy prednisone online without a prescription: 50 mg prednisone tablet – prednisone buy canada

  22. Stephenpem表示:

    http://buyprednisone.store/# prednisone 2.5 tablet

  23. DavidRak表示:

    prednisone over the counter uk canine prednisone 5mg no prescription prednisone online india

  24. Charlesdix表示:

    http://stromectol.fun/# stromectol 3mg

  25. DavidRak表示:

    zestril 30 mg lisinopril without rx lisinopril buy without prescription

  26. Charlesdix表示:

    http://buyprednisone.store/# prednisone cream over the counter

  27. JamesCof表示:

    amoxicillin buy no prescription: where can i buy amoxicillin over the counter uk – buy amoxicillin online no prescription

  28. Stephenpem表示:

    http://lisinopril.top/# lisinopril 10 mg brand name in india

  29. DavidRak表示:

    buy prednisone without prescription prednisone 5084 prednisone 40 mg price

  30. JamesCof表示:

    ivermectin over the counter canada: ivermectin 0.5% lotion – ivermectin stromectol

發佈留言

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