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

41,641 Responses

  1. BennyNib表示:

    online canadian pharmacy: canada drugs online – canadianpharmacy com

  2. Evjlax表示:

    buy furosemide 100mg order lasix 100mg generic lasix 100mg cost

  3. BennyNib表示:

    non prescription ed pills: generic cialis without a doctor prescription – ed prescription drugs

  4. BennyNib表示:

    buy prescription drugs online legally: ed pills without doctor prescription – prescription drugs canada buy online

  5. WilliamSoorp表示:

    http://edpill.cheap/# medicine for erectile

  6. Eugenehaupe表示:

    indianpharmacy com top online pharmacy india reputable indian pharmacies

  7. WilliamSoorp表示:

    https://edpill.cheap/# otc ed pills

  8. WilliamSoorp表示:

    https://medicinefromindia.store/# indian pharmacy online

  9. Eugenehaupe表示:

    pharmacy website india best india pharmacy buy medicines online in india

  10. Eugenehaupe表示:

    ed pills otc top erection pills best ed pills at gnc

  11. Eugenehaupe表示:

    ed pills that work non prescription ed pills ed remedies

  12. WilliamSoorp表示:

    https://edwithoutdoctorprescription.pro/# 100mg viagra without a doctor prescription

  13. WilliamSoorp表示:

    https://medicinefromindia.store/# reputable indian pharmacies

  14. Eugenehaupe表示:

    purple pharmacy mexico price list mexico drug stores pharmacies buying prescription drugs in mexico online

  15. WilliamSoorp表示:

    http://edwithoutdoctorprescription.pro/# buy prescription drugs online

  16. Eugenehaupe表示:

    buying from online mexican pharmacy mexico pharmacy mexican border pharmacies shipping to usa

  17. WilliamSoorp表示:

    https://canadianinternationalpharmacy.pro/# legal to buy prescription drugs from canada

  18. Eugenehaupe表示:

    buy prescription drugs online without generic cialis without a doctor prescription tadalafil without a doctor’s prescription

  19. Eugenehaupe表示:

    Online medicine home delivery reputable indian pharmacies Online medicine home delivery

  20. WilliamSoorp表示:

    http://canadianinternationalpharmacy.pro/# pet meds without vet prescription canada

  21. WilliamSoorp表示:

    https://edwithoutdoctorprescription.pro/# buy prescription drugs without doctor

  22. WilliamSoorp表示:

    http://canadianinternationalpharmacy.pro/# reputable canadian pharmacy

  23. BennyNib表示:

    canadian mail order pharmacy: canadian pharmacy 24 – canadian pharmacy reviews

  24. Eugenehaupe表示:

    indian pharmacy indian pharmacies safe india online pharmacy

  25. BennyNib表示:

    canadian world pharmacy: canada cloud pharmacy – my canadian pharmacy review

  26. Eugenehaupe表示:

    non prescription ed drugs cialis without a doctor prescription canada viagra without a doctor prescription

  27. WilliamSoorp表示:

    http://certifiedpharmacymexico.pro/# medication from mexico pharmacy

  28. Eugenehaupe表示:

    generic ed drugs erection pills viagra online erectile dysfunction medications

  29. BennyNib表示:

    medications for ed: ed dysfunction treatment – men’s ed pills

發佈留言

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