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

36,164 Responses

  1. JulioAxowl表示:

    mexican border pharmacies shipping to usa mexican border pharmacies shipping to usa mexican rx online

  2. TravisNOb表示:

    http://mexicanph.com/# mexican rx online
    mexico drug stores pharmacies

  3. Louishycle表示:

    pharmacies in mexico that ship to usa mexico drug stores pharmacies mexican online pharmacies prescription drugs

  4. TravisNOb表示:

    http://mexicanph.com/# best online pharmacies in mexico
    pharmacies in mexico that ship to usa

  5. JerryMig表示:

    medicine in mexico pharmacies reputable mexican pharmacies online medicine in mexico pharmacies

  6. TravisNOb表示:

    http://mexicanph.com/# reputable mexican pharmacies online
    mexican online pharmacies prescription drugs

  7. Louishycle表示:

    reputable mexican pharmacies online medication from mexico pharmacy mexico pharmacy

  8. JerryMig表示:

    mexico drug stores pharmacies buying prescription drugs in mexico online mexico drug stores pharmacies

  9. TravisNOb表示:

    https://mexicanph.shop/# mexican pharmaceuticals online
    mexican mail order pharmacies

  10. JerryMig表示:

    mexico drug stores pharmacies buying from online mexican pharmacy mexican pharmaceuticals online

  11. Louishycle表示:

    purple pharmacy mexico price list reputable mexican pharmacies online mexican online pharmacies prescription drugs

  12. JerryMig表示:

    buying from online mexican pharmacy mexico drug stores pharmacies buying prescription drugs in mexico online

  13. Eugenehaupe表示:

    legitimate canadian online pharmacies trustworthy canadian pharmacy canadian pharmacy sarasota

  14. WilliamSoorp表示:

    https://medicinefromindia.store/# india pharmacy mail order

  15. Tcyumh表示:

    order rybelsus 14mg online cheap buy semaglutide no prescription rybelsus 14 mg sale

  16. Eugenehaupe表示:

    non prescription ed pills pills erectile dysfunction medicine for impotence

  17. WilliamSoorp表示:

    http://certifiedpharmacymexico.pro/# mexican rx online

  18. Eugenehaupe表示:

    п»їbest mexican online pharmacies buying prescription drugs in mexico online reputable mexican pharmacies online

  19. WilliamSoorp表示:

    https://edwithoutdoctorprescription.pro/# real cialis without a doctor’s prescription

  20. Eugenehaupe表示:

    mexican pharmacy without prescription cheap cialis ed meds online without doctor prescription

  21. WilliamSoorp表示:

    https://certifiedpharmacymexico.pro/# mexican drugstore online

  22. BennyNib表示:

    online shopping pharmacy india: indian pharmacies safe – reputable indian online pharmacy

  23. WilliamSoorp表示:

    https://canadianinternationalpharmacy.pro/# 77 canadian pharmacy

  24. Rqvgdm表示:

    doxycycline 100mg usa order doxycycline 200mg online cheap cheap acticlate

  25. BennyNib表示:

    viagra without a doctor prescription: cialis without a doctor prescription – viagra without doctor prescription

  26. WilliamSoorp表示:

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

  27. BennyNib表示:

    canada pharmacy: canada pharmacy online – canadian pharmacy 365

  28. WilliamSoorp表示:

    http://certifiedpharmacymexico.pro/# mexican online pharmacies prescription drugs

  29. Eugenehaupe表示:

    legal to buy prescription drugs from canada generic cialis without a doctor prescription buy prescription drugs without doctor

  30. BennyNib表示:

    canadian pharmacy world: canadian pharmacies – canadian pharmacy king reviews

發佈留言

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