透過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...

48,226 Responses

  1. Danieljek表示:

    amoxicillin discount coupon https://prednisoneraypharm.com/# prednisone 60 mg tablet

  2. Тут можно преобрести сейф для оружия цена купить интернет магазин сейфов для оружия

  3. Danieljek表示:

    generic amoxicillin 500mg http://clomidrexpharm.com/# can i order generic clomid pill

  4. Bradleygoath表示:

    buy amoxicillin online without prescription cheap amoxil amoxicillin 825 mg

  5. BrandenSlomy表示:

    priligy maxpharm: buy dapoxetine online – priligy

  6. Danieljek表示:

    amoxicillin 500mg capsule http://priligymaxpharm.com/# dapoxetine price

  7. Dereketerm表示:

    prednisone 15 mg daily: generic Prednisone – prednisone best prices

  8. Bradleygoath表示:

    by prednisone w not prescription cheap prednisone prednisone over the counter uk

  9. StevenNub表示:

    where can i buy amoxocillin: Amoxicillin buy online – amoxicillin 500 mg without a prescription

  10. BrandenSlomy表示:

    amoxicillin 500mg tablets price in india: amoxil com pharm – price of amoxicillin without insurance

  11. Danieljek表示:

    can you purchase amoxicillin online https://clomidrexpharm.com/# buying generic clomid without insurance

  12. Dereketerm表示:

    can i buy clomid prices: clomid purchase online rex pharm – cost cheap clomid without dr prescription

  13. Bradleygoath表示:

    how to buy cheap clomid online cheap clomid buying cheap clomid without dr prescription

  14. Dereketerm表示:

    cost generic clomid pills: clomid online – how to buy clomid without rx

  15. Danieljek表示:

    amoxicillin 30 capsules price http://prednisoneraypharm.com/# can i purchase prednisone without a prescription

  16. Bradleygoath表示:

    buy generic clomid without dr prescription buy clomid get clomid now

  17. StevenNub表示:

    how to buy generic clomid: cheap clomid – can i get clomid price

  18. Dereketerm表示:

    buy amoxil: buy amoxil online – buy amoxicillin from canada

  19. StevenNub表示:

    amoxicillin discount coupon: Amoxicillin buy online – generic amoxicillin over the counter

  20. Dereketerm表示:

    prednisone buy online nz: prednisone ray pharm – prednisone online

  21. Danieljek表示:

    generic amoxil 500 mg https://clomidrexpharm.com/# cost of generic clomid pills

  22. Danieljek表示:

    order amoxicillin online http://priligymaxpharm.com/# cheap priligy

  23. BrandenSlomy表示:

    dapoxetine price: dapoxetine price – buy priligy max pharm

  24. StevenNub表示:

    iv prednisone: Prednisone Without Prescription – generic prednisone for sale

  25. BrandenSlomy表示:

    amoxicillin 500 mg tablet price: Amoxicillin for sale – generic amoxicillin 500mg

  26. StevenNub表示:

    amoxicillin without rx: can you buy amoxicillin over the counter in canada – amoxicillin 500 capsule

  27. BrandenSlomy表示:

    prednisone 30 mg: prednisone 10mg tablet price – cost of prednisone 10mg tablets

  28. Thi Kvam表示:

    Merci j’ai appris beaucoup avec ton article 🙂 !

發佈留言

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