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

  1. LeonardFaulk表示:

    lisinopril 40 mg no prescription: zestril 30 mg – zestril 5 mg india

  2. JamesCof表示:

    prinivil cost: lisinopril generic price in india – lisinopril 1 mg tablet

  3. JamesCof表示:

    furosemide 40 mg: Buy Lasix No Prescription – lasix side effects

  4. Charlesdix表示:

    http://lisinopril.top/# 2 lisinopril

  5. Charlesdix表示:

    https://furosemide.guru/# lasix generic

  6. LeonardFaulk表示:

    lisinopril 40 mg brand name in india: prinivil 10 mg tablet – order lisinopril from mexico

  7. Stephenpem表示:

    http://buyprednisone.store/# prednisone buy cheap

  8. JamesCof表示:

    lisinopril 80 mg: lisinopril 420 1g – 50mg lisinopril

  9. DavidRak表示:

    lisinopril 12.5 20 g lisinopril 5 mg buy lisinopril 120 mg

  10. DavidRak表示:

    generic prednisone 10mg prednisone price prednisone 10mg tabs

  11. Wsoeoa表示:

    zovirax tablet zyloprim online allopurinol 300mg for sale

  12. JamesCof表示:

    generic amoxicillin 500mg: amoxil generic – amoxicillin 500mg no prescription

  13. Charlesdix表示:

    https://amoxil.cheap/# amoxicillin generic

  14. Charlesdix表示:

    http://amoxil.cheap/# where to buy amoxicillin

  15. JamesCof表示:

    where can you buy prednisone: prednisone 20 mg – canada buy prednisone online

  16. DavidRak表示:

    price of ivermectin liquid ivermectin 9 mg tablet stromectol price uk

  17. Stephenpem表示:

    http://stromectol.fun/# ivermectin 1%

  18. LeonardFaulk表示:

    ivermectin 15 mg: ivermectin 3 mg tablet dosage – stromectol tablets uk

  19. JamesCof表示:

    stromectol cvs: ivermectin 10 mg – buy ivermectin for humans uk

  20. Olxfzj表示:

    amlodipine where to buy buy amlodipine 10mg pills amlodipine cheap

  21. JamesCof表示:

    prednisone cost 10mg: buy prednisone 10 mg – buy prednisone 1 mg mexico

  22. Charlesdix表示:

    http://lisinopril.top/# zestril 10

  23. Charlesdix表示:

    http://lisinopril.top/# lisinopril tab 5 mg price

  24. DavidRak表示:

    buy ivermectin generic name for ivermectin ivermectin tablets uk

  25. JamesCof表示:

    buy amoxicillin: amoxicillin over counter – amoxicillin 500mg capsules price

  26. DavidRak表示:

    generic lasix Buy Lasix lasix dosage

  27. LeonardFaulk表示:

    over the counter prednisone cheap: prednisone for sale without a prescription – prednisone 5 mg tablet

  28. Stephenpem表示:

    https://amoxil.cheap/# where can you buy amoxicillin over the counter

  29. Charlesdix表示:

    http://amoxil.cheap/# amoxicillin 500mg price in canada

發佈留言

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