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

  1. LeonardFaulk表示:

    buy lasix online: Over The Counter Lasix – lasix generic name

  2. JamesCof表示:

    stromectol tablets: order stromectol online – stromectol

  3. Charlesdix表示:

    http://furosemide.guru/# furosemida 40 mg

  4. Charlesdix表示:

    https://furosemide.guru/# buy lasix online

  5. Stephenpem表示:

    https://amoxil.cheap/# amoxicillin 500 mg online

  6. JamesCof表示:

    prednisone 5 mg: where to buy prednisone uk – prednisone 5 50mg tablet price

  7. DavidRak表示:

    zestril price in india where to buy lisinopril online lisinopril 20 mg canadian pharmacy

  8. DavidRak表示:

    amoxicillin 1000 mg capsule amoxicillin 500 mg capsule purchase amoxicillin 500 mg

  9. LeonardFaulk表示:

    prednisone 20 mg pill: cost of prednisone 40 mg – 20 mg prednisone tablet

  10. JamesCof表示:

    buy furosemide online: Over The Counter Lasix – furosemida

  11. Charlesdix表示:

    http://furosemide.guru/# lasix for sale

  12. Charlesdix表示:

    http://stromectol.fun/# ivermectin otc

  13. DavidRak表示:

    lisinopril 20mg discount lisinopril 80 mg daily lisinopril over the counter

  14. JamesCof表示:

    furosemida: Buy Lasix No Prescription – lasix

  15. Stephenpem表示:

    http://stromectol.fun/# buy minocycline

  16. JamesCof表示:

    cost of prednisone 40 mg: can you buy prednisone over the counter – cost of prednisone 5mg tablets

  17. LeonardFaulk表示:

    lisinopril 5 mg canada: lisinopril australia – zestoretic 5 mg

  18. Charlesdix表示:

    https://buyprednisone.store/# prednisone 10 mg over the counter

  19. Charlesdix表示:

    https://stromectol.fun/# cost of stromectol

  20. JamesCof表示:

    prednisone for sale: prednisone 12 mg – can you buy prednisone in canada

  21. DavidRak表示:

    lasix pills Over The Counter Lasix lasix 20 mg

  22. DavidRak表示:

    prednisone 50 mg tablet cost prednisone 1mg purchase prednisone 10 mg

  23. Stephenpem表示:

    http://furosemide.guru/# lasix tablet

  24. JamesCof表示:

    generic name for ivermectin: purchase stromectol – stromectol

  25. Dfkxey表示:

    chloroquine 250mg pills buy aralen online cheap buy chloroquine 250mg generic

  26. Charlesdix表示:

    http://stromectol.fun/# ivermectin usa price

  27. DavidRak表示:

    50 mg lisinopril lisinopril price comparison lisinopril 20mg prices

  28. Charlesdix表示:

    https://amoxil.cheap/# order amoxicillin online no prescription

  29. LeonardFaulk表示:

    amoxicillin 500mg tablets price in india: where to buy amoxicillin – amoxicillin buy online canada

  30. JamesCof表示:

    prednisone cost canada: prednisone drug costs – prednisone 10mg canada

發佈留言

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