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

  1. Edwardjem表示:

    cost clomid price: get clomid now – buying clomid prices

  2. RobertVex表示:

    mexican pharmaceuticals online: cheapest mexico drugs – buying prescription drugs in mexico online

  3. Charlesutisa表示:

    buy prescription drugs from india: Healthcare and medicines from India – buy medicines online in india

  4. Edwardalorp表示:

    pharmacy website india indian pharmacy india pharmacy mail order

  5. GeraldSkese表示:

    best online pharmacy india: indian pharmacy – reputable indian online pharmacy

  6. Edwardalorp表示:

    Online medicine order Cheapest online pharmacy pharmacy website india

  7. Edwardalorp表示:

    buying from online mexican pharmacy mexico pharmacy mexico pharmacies prescription drugs

  8. GeraldSkese表示:

    mexican rx online: mexican pharmacy – mexico drug stores pharmacies

  9. GeraldSkese表示:

    vipps canadian pharmacy: Prescription Drugs from Canada – onlinepharmaciescanada com

  10. Charlesutisa表示:

    india online pharmacy: Generic Medicine India to USA – india pharmacy mail order

  11. Edwardalorp表示:

    mexican drugstore online Online Pharmacies in Mexico mexico pharmacies prescription drugs

  12. GeraldSkese表示:

    mexican online pharmacies prescription drugs: cheapest mexico drugs – best online pharmacies in mexico

  13. Edwardalorp表示:

    canadian drugs online legitimate canadian pharmacies canadian pharmacy online

  14. GeraldSkese表示:

    reliable canadian pharmacy reviews: Large Selection of Medications – canadian online pharmacy

  15. MichaelFedly表示:

    https://indianpharmacy.icu/# indian pharmacies safe

  16. RobertVex表示:

    top 10 pharmacies in india: indian pharmacy – п»їlegitimate online pharmacies india

  17. Edwardalorp表示:

    mexican online pharmacies prescription drugs Online Pharmacies in Mexico mexican pharmaceuticals online

  18. MichaelFedly表示:

    https://mexicanpharmacy.shop/# medication from mexico pharmacy

  19. RobertVex表示:

    mexico drug stores pharmacies: Mexican Pharmacy Online – purple pharmacy mexico price list

  20. MichaelFedly表示:

    http://mexicanpharmacy.shop/# mexican pharmaceuticals online

  21. Edwardalorp表示:

    world pharmacy india Generic Medicine India to USA Online medicine order

  22. Edwardalorp表示:

    Online medicine order Generic Medicine India to USA п»їlegitimate online pharmacies india

  23. Charlesutisa表示:

    reputable indian online pharmacy: Cheapest online pharmacy – indian pharmacy

  24. Edwardalorp表示:

    canada drugstore pharmacy rx Large Selection of Medications canadian pharmacy ltd

  25. MichaelFedly表示:

    https://mexicanpharmacy.shop/# medicine in mexico pharmacies

  26. Charlesutisa表示:

    reliable canadian pharmacy: Large Selection of Medications – canada pharmacy online

  27. RobertVex表示:

    indian pharmacy online: indian pharmacy – india online pharmacy

  28. Edwardalorp表示:

    top 10 online pharmacy in india indian pharmacy delivery india pharmacy mail order

  29. Edwardalorp表示:

    mexican pharmacy cheapest mexico drugs mexican online pharmacies prescription drugs

  30. RobertVex表示:

    mexican border pharmacies shipping to usa: purple pharmacy mexico price list – mexico pharmacy

發佈留言

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