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

  1. JamesCof表示:

    amoxicillin 875 125 mg tab: generic amoxicillin 500mg – amoxicillin 500mg capsule cost

  2. Charlesdix表示:

    http://buyprednisone.store/# how much is prednisone 5mg

  3. DavidRak表示:

    stromectol for head lice cost of ivermectin pill ivermectin 0.5%

  4. checkfsm ivermectin rabbits inspected ivermectin kills what worms what is the appropriate dosage of stromectol
    – buy stromectol for humans in tijuana [url=https://canadapharmacy-usa.com/buy-stromectol-usa.html]ivermectin dogs[/url]
    lavalder ivermectin overdose

  5. JamesCof表示:

    amoxicillin 500mg tablets price in india: amoxicillin online pharmacy – over the counter amoxicillin canada

  6. JamesCof表示:

    prednisone 20: prednisone 54899 – prednisone 60 mg daily

  7. LeonardFaulk表示:

    lisinopril 40 mg price: lisinopril 20 mg tablets – lisinopril 2016

  8. Charlesdix表示:

    http://furosemide.guru/# lasix medication

  9. Charlesdix表示:

    http://buyprednisone.store/# prednisone online for sale

  10. JamesCof表示:

    prednisone acetate: prednisone 40 mg – prednisone canada pharmacy

  11. DavidRak表示:

    lasix medication buy lasix online lasix pills

  12. DavidRak表示:

    cost of lisinopril lisinopril 40 mg india lisinopril 20 mg tabs

  13. JamesCof表示:

    ivermectin human: ivermectin 1% – buy ivermectin cream

  14. Stephenpem表示:

    http://buyprednisone.store/# buy prednisone 50 mg

  15. Charlesdix表示:

    http://stromectol.fun/# cost of ivermectin cream

  16. LeonardFaulk表示:

    lisinopril tablet: lisinopril 10 mg tabs – lisinopril 120mg

  17. JamesCof表示:

    ivermectin 1 cream: ivermectin cost canada – ivermectin 250ml

  18. Charlesdix表示:

    https://furosemide.guru/# lasix side effects

  19. DavidRak表示:

    lasix online Buy Lasix lasix dosage

  20. JamesCof表示:

    amoxicillin 500mg capsules antibiotic: amoxicillin 500mg capsules antibiotic – cost of amoxicillin

  21. Great site! I recommend to everyone!investing in us stocks

  22. Stephenpem表示:

    http://lisinopril.top/# best lisinopril brand

  23. Charlesdix表示:

    http://lisinopril.top/# lisinopril 40 mg tablet

  24. JamesCof表示:

    lasix 100 mg: furosemide – lasix side effects

  25. Charlesdix表示:

    http://furosemide.guru/# lasix online

  26. LeonardFaulk表示:

    prednisone 2.5 tablet: prednisone in india – how much is prednisone 10 mg

  27. JamesCof表示:

    ivermectin 18mg: ivermectin medication – ivermectin 0.1

  28. DavidRak表示:

    furosemide 100 mg Buy Lasix No Prescription lasix for sale

  29. DavidRak表示:

    stromectol 3mg buy stromectol canada ivermectin 250ml

  30. Charlesdix表示:

    https://buyprednisone.store/# prednisone 10 mg brand name

發佈留言

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