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

  1. JustinTam表示:

    amoxicillin 500 mg without a prescription: AmoHealthPharm – buying amoxicillin online

  2. VirgilBlawl表示:

    zithromax buy zithromax online how to get zithromax online

  3. BrandonJag表示:

    where to buy amoxicillin pharmacy: amoxicillin 500 mg where to buy – where can i get amoxicillin

  4. Briantrozy表示:

    zithromax online paypal: order zithromax over the counter – zithromax cost

  5. VirgilBlawl表示:

    buy zithromax how much is zithromax 250 mg where to get zithromax

  6. Briantrozy表示:

    buying doxycycline online in usa: doxycycline 100mg over the counter – purchase doxycycline online uk

  7. BrandonJag表示:

    buy doxycycline without prescription uk: DoxHealthPharm – doxycycline 500mg tablets

  8. Roberttic表示:

    https://zithropharm.com/# azithromycin zithromax

  9. Amako, Uche (7 August 2015).

  10. BrandonJag表示:

    zithromax 500 mg for sale: ZithroPharm – zithromax 250

  11. Poker casino表示:

    Feel free to visit my site :: Poker casino

  12. Briantrozy表示:

    clomid medication: where to buy generic clomid without insurance – where to get clomid no prescription

  13. JustinTam表示:

    doxycycline 100mg cap tab: DoxHealthPharm – doxycycline 75 mg

  14. Richardfloda表示:

    Вакансии Москва – Барахолка Москва, Погода в Москве

  15. Richardfloda表示:

    Моя Москва – Вакансии Москва, Вакансии Москва

  16. Roberttic表示:

    http://doxhealthpharm.com/# doxycycline 1000mg

  17. VirgilBlawl表示:

    doxycycline online pharmacy canada doxycycline 100mg lowest price doxycycline cost canada

  18. BrandonJag表示:

    buy zithromax online: Zithro Pharm – zithromax 500 mg lowest price drugstore online

  19. Briantrozy表示:

    buy doxycycline online 270 tabs: doxycycline discount – doxycycline online sale

  20. Roberttic表示:

    https://zithropharm.shop/# zithromax 500mg price in india

  21. VirgilBlawl表示:

    buy doxycycline in usa Dox Health Pharm doxycycline generic pharmacy

  22. Briantrozy表示:

    3626 doxycycline: DoxHealthPharm – doxycycline 60 mg

  23. Richardfloda表示:

    Работа Москва – Город Москва, Время в Москве

  24. VirgilBlawl表示:

    where to buy generic clomid without insurance where buy cheap clomid without insurance buying clomid for sale

  25. JustinTam表示:

    doxycycline 200 mg price in india: Dox Health Pharm – where to get doxycycline

  26. JustinTam表示:

    get cheap clomid no prescription: where buy generic clomid tablets – can you buy cheap clomid

  27. If we had waited until beta to make cuts, with only a few months to go earlier than our ship date (as many builders do), it will have been a disaster.

  28. Briantrozy表示:

    zithromax cost australia: Zithro Pharm – where can i buy zithromax uk

  29. Briantrozy表示:

    zithromax z-pak price without insurance: Zithro Pharm – buy zithromax online australia

  30. BrandonJag表示:

    doxycycline 3142: DoxHealthPharm – doxycycline 1000 mg best buy

發佈留言

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