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

  1. DavidRak表示:

    ivermectin 0.08% ivermectin human stromectol 15 mg

  2. LeonardFaulk表示:

    how much is amoxicillin prescription: amoxicillin pharmacy price – can i purchase amoxicillin online

  3. Charlesdix表示:

    http://buyprednisone.store/# buy prednisone without a prescription

  4. JamesCof表示:

    prednisone 30: 2.5 mg prednisone daily – prednisone 20mg online without prescription

  5. Charlesdix表示:

    http://stromectol.fun/# stromectol tablets for humans for sale

  6. DavidRak表示:

    cost of stromectol ivermectin australia stromectol 3 mg

  7. JamesCof表示:

    prednisone 50: prednisone online – how much is prednisone 10 mg

  8. Stephenpem表示:

    http://furosemide.guru/# lasix 100 mg tablet

  9. JamesCof表示:

    cost of lisinopril 10 mg: buy prinivil – zestril price uk

  10. Charlesdix表示:

    https://buyprednisone.store/# prednisone 0.5 mg

  11. LeonardFaulk表示:

    buy oral ivermectin: ivermectin price usa – ivermectin 3mg tab

  12. Charlesdix表示:

    http://lisinopril.top/# zestoretic 20 25mg

  13. JamesCof表示:

    stromectol 3 mg: ivermectin 15 mg – ivermectin cream 1%

  14. DavidRak表示:

    lasix furosemide 40 mg Buy Lasix lasix 20 mg

  15. JamesCof表示:

    prednisone without a prescription: prednisone 20 mg tablet – prednisone 10mg buy online

  16. DavidRak表示:

    where to buy lisinopril without prescription cost of generic lisinopril 10 mg purchase lisinopril 10 mg

  17. Charlesdix表示:

    http://lisinopril.top/# cost of lisinopril 40mg

  18. Charlesdix表示:

    https://stromectol.fun/# purchase stromectol

  19. JamesCof表示:

    where to buy stromectol: where to buy stromectol – ivermectin 90 mg

  20. LeonardFaulk表示:

    ivermectin generic cream: stromectol for sale – ivermectin 12

  21. DavidRak表示:

    stromectol drug ivermectin cost stromectol ivermectin buy

  22. Vdwjqa表示:

    order glycomet 1000mg online cheap purchase glycomet generic metformin 500mg sale

  23. JamesCof表示:

    prednisone cream: prednisone medicine – prednisone 2.5 mg daily

  24. Great site! I recommend to everyone!top us stocks

  25. Stephenpem表示:

    https://amoxil.cheap/# buy amoxicillin over the counter uk

  26. JamesCof表示:

    prednisone 30 mg daily: prednisone buy – prednisone buy without prescription

  27. Charlesdix表示:

    https://lisinopril.top/# prescription drug zestril

  28. LeonardFaulk表示:

    over the counter amoxicillin: cost of amoxicillin prescription – order amoxicillin online no prescription

  29. JamesCof表示:

    generic prednisone pills: buy prednisone online from canada – non prescription prednisone 20mg

  30. DavidRak表示:

    how much does ivermectin cost stromectol ireland ivermectin oral

發佈留言

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