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

  1. Jerrybem表示:

    https://mexicanpharm.shop/# mexican rx online mexicanpharm.shop

  2. TimothyTUt表示:

    canadian pharmacy price checker Canadian International Pharmacy canadian drug prices canadianpharm.store

  3. RamonHom表示:

    canadian pharmacy online: ed meds online canada – canadian pharmacies that deliver to the us canadianpharm.store

  4. MichaelRof表示:

    canadian pharmacy store: Canadian Pharmacy – canada pharmacy online legit canadianpharm.store

  5. TimothyTUt表示:

    buying prescription drugs in mexico online Online Pharmacies in Mexico medication from mexico pharmacy mexicanpharm.shop

  6. Jerrybem表示:

    http://canadianpharm.store/# pharmacies in canada that ship to the us canadianpharm.store

  7. TimothyTUt表示:

    india pharmacy mail order order medicine from india to usa india pharmacy mail order indianpharm.store

  8. Charlesven表示:

    reputable indian pharmacies: order medicine from india to usa – reputable indian pharmacies indianpharm.store

  9. Charlesven表示:

    medication canadian pharmacy: Certified Online Pharmacy Canada – legitimate canadian online pharmacies canadianpharm.store

  10. Oliverdract表示:

    where can i get zithromax zithromax z-pak generic zithromax online paypal

  11. Oliverdract表示:

    azithromycin zithromax where can i get zithromax over the counter zithromax 500 mg

  12. Andrewwaize表示:

    http://amoxicillin.bid/# buy amoxicillin 500mg online

  13. AaronduH表示:

    amoxicillin 500 mg price: amoxicillin without a doctors prescription – amoxicillin generic brand

  14. Всем рекомендую!Тесты МТИ

  15. AaronduH表示:

    stromectol 3mg tablets: buy ivermectin pills – ivermectin 5ml

  16. Andrewwaize表示:

    https://clomiphene.icu/# where buy generic clomid for sale

  17. ChrisCab表示:

    ivermectin iv: ivermectin oral – ivermectin lotion cost

  18. Andrewwaize表示:

    http://clomiphene.icu/# get cheap clomid

  19. Jameskig表示:

    http://amoxicillin.bid/# cost of amoxicillin prescription

  20. Jameskig表示:

    https://prednisonetablets.shop/# prescription prednisone cost

  21. Oliverdract表示:

    stromectol online canada ivermectin ireland stromectol pills

  22. ChrisCab表示:

    buy prednisone from canada: can you buy prednisone over the counter – medicine prednisone 5mg

  23. Andrewwaize表示:

    http://clomiphene.icu/# cost generic clomid for sale

  24. AaronduH表示:

    amoxicillin 500 mg where to buy: amoxicillin 775 mg – amoxicillin 500mg without prescription

  25. Oliverdract表示:

    buy zithromax online fast shipping buy zithromax no prescription purchase zithromax z-pak

  26. AaronduH表示:

    amoxicillin capsules 250mg: amoxicillin 500mg capsule – antibiotic amoxicillin

  27. Andrewwaize表示:

    http://clomiphene.icu/# how to get generic clomid online

  28. Oliverdract表示:

    amoxicillin 500 mg online where to buy amoxicillin pharmacy amoxicillin generic brand

  29. Pdgfob表示:

    purchase semaglutide without prescription order rybelsus online rybelsus ca

  30. AaronduH表示:

    prednisone without prescription.net: buy prednisone without a prescription – prednisone 20mg for sale

發佈留言

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