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

  1. BrandonJag表示:

    buy amoxicillin online with paypal: generic amoxicillin – amoxicillin buy no prescription

  2. Roberttic表示:

    http://zithropharm.com/# where can i buy zithromax uk

  3. JjhfhgxyWar表示:

    Кодирование от алкоголизма в Барнауле в частной клинике «Рехаб22» – это эффективное и анонимное лечение с гарантией результата. [url=https://rehab-22.ru/alkogolizm-categories/kodirovanie/]кодировка барнаул rehab-22[/url]

  4. JustinTam表示:

    can i get cheap clomid pills: ClmHealthPharm – clomid otc

  5. JustinTam表示:

    order cheap clomid for sale: generic clomid no prescription – can i get clomid without insurance

  6. VirgilBlawl表示:

    can i order cheap clomid without a prescription ClmHealthPharm where to buy generic clomid without dr prescription

  7. Briantrozy表示:

    amoxicillin over counter: can i buy amoxicillin over the counter in australia – order amoxicillin online

  8. Briantrozy表示:

    how to get generic clomid for sale: can you get cheap clomid without a prescription – can you get clomid online

  9. BrandonJag表示:

    zithromax 600 mg tablets: ZithroPharm – generic zithromax 500mg india

  10. Briantrozy表示:

    amoxicillin 775 mg: purchase amoxicillin online – amoxicillin 500 mg purchase without prescription

  11. VirgilBlawl表示:

    amoxicillin 500 mg tablet price amoxicillin medicine over the counter buy amoxicillin

  12. iuzvijowf表示:

    Balloon Boom es un juego de estrategia y habilidad que ha capturado la atención de muchos jugadores en línea. Aunque su mecánica es simple, el juego requiere una combinación de lógica y destreza para predecir cuándo los globos explotarán. En este artículo, exploraremos los secretos detrás de este juego y cómo puedes mejorar tus habilidades para dominarlo. I tried out the Balloon game at a few online casinos in Chile. The game is easy to understand and play. The potential winnings are quite attractive. However, I didn’t find it to be particularly exciting or unique. It’s a decent game, but there are other online casino games that I enjoy more. I think it ultimately comes down to personal preference. If you’re a fan of simple yet potentially lucrative games, then Disfruta del emocionante juego de Balloon y gana en grande en casinos en línea de Chile might be worth checking out.
    https://test.apppoker.deals/uncategorized/entrada-alrededor-pensamiento-sobre-balloon-app-de-lucro/
    Es un juego online en el que los jugadores hacen apuestas e inflan un globo. El objetivo del juego es retirar dinero antes de que el globo explote. Se puede jugar por dinero real o sólo por diversión. Balloon App Ganar Dinero Cada ronda de Balloon App te enfrenta a decisiones rápidas y estratégicas. El riesgo de perder aumenta a medida que el globo se infla más, lo que aporta emoción a cada momento. Es un juego donde la estrategia de cuándo retirarte puede marcar la diferencia entre ganar grandes premios o perderlo todo. Sí, Balloon 1Win app ofrece a sus usuarios la posibilidad de realizar depósitos y retiradas en criptomoneda. 1win Chile retirar dinero es un proceso sencillo y seguro para todos los usuarios. La plataforma ofrece múltiples métodos para retirar dinero de 1 win, garantizando rapidez y eficiencia.

  13. VirgilBlawl表示:

    doxycycline cheap uk doxycycline cheap doxycycline pills buy

  14. Roberttic表示:

    https://amohealthpharm.shop/# amoxicillin medicine

  15. BrandonJag表示:

    zithromax online pharmacy canada: Zithro Pharm – where to get zithromax over the counter

  16. BrandonJag表示:

    doxycycline price usa: DoxHealthPharm – doxycycline price

  17. Briantrozy表示:

    buy generic zithromax no prescription: Zithro Pharm – zithromax tablets

  18. JustinTam表示:

    zithromax cost australia: zithromax capsules 250mg – can i buy zithromax online

  19. VirgilBlawl表示:

    buy zithromax without prescription online ZithroPharm zithromax online

  20. Briantrozy表示:

    can you buy zithromax over the counter in mexico: zithromax drug – zithromax 500 mg lowest price pharmacy online

  21. Roberttic表示:

    https://clmhealthpharm.com/# where to buy cheap clomid prices

  22. Roberttic表示:

    https://zithropharm.shop/# how much is zithromax 250 mg

  23. BrandonJag表示:

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

  24. Briantrozy表示:

    amoxicillin capsules 250mg: buy amoxicillin from canada – buy amoxicillin 250mg

  25. Briantrozy表示:

    can you buy zithromax over the counter in canada: Zithro Pharm – can i buy zithromax over the counter

  26. JustinTam表示:

    can i buy zithromax online: Zithro Pharm – order zithromax without prescription

發佈留言

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