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

52,175 Responses

  1. BrandonJag表示:

    zithromax 250 mg australia: zithromax 500 mg lowest price drugstore online – buy zithromax without prescription online

  2. Briantrozy表示:

    zithromax 500: where can i buy zithromax capsules – zithromax online usa no prescription

  3. JustinTam表示:

    amoxicillin from canada: AmoHealthPharm – amoxicillin in india

  4. Briantrozy表示:

    cheap clomid no prescription: ClmHealthPharm – can you get clomid for sale

  5. VirgilBlawl表示:

    where can i buy zithromax uk ZithroPharm zithromax 250mg

  6. BrandonJag表示:

    zithromax online australia: zithromax order online uk – can you buy zithromax over the counter in mexico

  7. Briantrozy表示:

    amoxicillin 200 mg tablet: buy amoxicillin online uk – amoxicillin 500 mg tablet

  8. Roberttic表示:

    https://amohealthpharm.com/# buy amoxicillin online no prescription

  9. Briantrozy表示:

    amoxicillin online pharmacy: amoxil generic – amoxicillin 500 mg tablets

  10. JustinTam表示:

    amoxicillin no prescipion: AmoHealthPharm – rexall pharmacy amoxicillin 500mg

  11. JustinTam表示:

    zithromax generic price: ZithroPharm – zithromax 500 tablet

  12. VirgilBlawl表示:

    can you buy cheap clomid where can i get clomid pill how can i get cheap clomid without prescription

  13. Roberttic表示:

    http://zithropharm.com/# zithromax capsules 250mg

  14. VirgilBlawl表示:

    zithromax prescription online Zithro Pharm zithromax 500 price

  15. BrandonJag表示:

    amoxicillin 500mg without prescription: where to buy amoxicillin – buy amoxicillin online mexico

  16. JustinTam表示:

    antibiotic amoxicillin: can you buy amoxicillin over the counter – amoxicillin 500mg for sale uk

  17. Briantrozy表示:

    can you buy zithromax over the counter in mexico: zithromax tablets for sale – zithromax 500mg over the counter

  18. Briantrozy表示:

    buy amoxicillin from canada: amoxicillin order online no prescription – amoxicillin in india

  19. BrandonJag表示:

    how can i get cheap clomid without dr prescription: ClmHealthPharm – cost cheap clomid online

  20. VirgilBlawl表示:

    can you buy amoxicillin over the counter amoxicillin 500mg amoxicillin 500mg price canada

  21. BrandonJag表示:

    generic amoxil 500 mg: AmoHealthPharm – cost of amoxicillin 875 mg

  22. Briantrozy表示:

    buy generic zithromax no prescription: Zithro Pharm – buy zithromax without presc

  23. JustinTam表示:

    generic clomid pills: ClmHealthPharm – can i buy generic clomid no prescription

  24. Briantrozy表示:

    generic clomid without a prescription: ClmHealthPharm – can i order generic clomid without dr prescription

  25. Briantrozy表示:

    zithromax 500mg over the counter: buy azithromycin zithromax – zithromax 250

  26. VirgilBlawl表示:

    zithromax 500mg price in india Zithro Pharm can you buy zithromax online

  27. Roberttic表示:

    http://zithropharm.com/# generic zithromax online paypal

  28. Briantrozy表示:

    order amoxicillin online uk: AmoHealthPharm – amoxicillin 500mg price in canada

  29. VirgilBlawl表示:

    zithromax 500 mg ZithroPharm zithromax capsules 250mg

  30. BrandonJag表示:

    zithromax 500 mg lowest price online: where can i get zithromax over the counter – can you buy zithromax over the counter in canada

發佈留言

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