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

  1. Henryfet表示:

    purchase amoxicillin online: amoxicillin online purchase – amoxicillin 500

  2. Billyved表示:

    https://amoxila.pro/# buy amoxicillin online uk

  3. Charleselila表示:

    where to buy zithromax in canada: how to buy zithromax online – zithromax prescription online

  4. Passando para a melhor casa de apostas 1win

  5. Melhor site de apostas 1win

  6. Richardorele表示:

    where can i buy zithromax medicine: buy zithromax online fast shipping – zithromax generic cost

  7. MarvinRoave表示:

    neurontin 100mg tablet neurontin 300mg tablet cost neurontin 600 mg price

  8. Billyved表示:

    https://prednisoned.online/# can you buy prednisone over the counter in canada

  9. Richardorele表示:

    buy doxycycline online without prescription: doxy 200 – order doxycycline online

  10. Henryfet表示:

    order doxycycline online: doxycycline order online – buy doxycycline

  11. Richardorele表示:

    where to buy amoxicillin: amoxicillin 250 mg – amoxicillin 800 mg price

  12. Billyved表示:

    http://doxycyclinea.online/# how to buy doxycycline online

  13. MarvinRoave表示:

    doxycycline 500mg doxycycline tetracycline price of doxycycline

  14. Henryfet表示:

    neurontin 50mg tablets: canada where to buy neurontin – neurontin prescription medication

  15. Richardorele表示:

    30mg prednisone: can i buy prednisone online without a prescription – prednisone cost us

  16. MarvinRoave表示:

    prednisone without a prescription buy prednisone mexico prednisone 12 mg

  17. MarvinRoave表示:

    prednisone 20 mg prices prednisone online 1 mg prednisone daily

  18. Henryfet表示:

    zithromax online australia: order zithromax without prescription – where to get zithromax

  19. Billyved表示:

    https://zithromaxa.store/# buy zithromax online australia

  20. Henryfet表示:

    neurontin medicine: neurontin capsules 600mg – neurontin prescription medication

  21. Billyved表示:

    https://amoxila.pro/# can you purchase amoxicillin online

  22. Отличный сайт! Тут Вы можете найтиновые сервера майнкрафт

  23. MarvinRoave表示:

    zithromax 250 mg zithromax 500 mg for sale zithromax online no prescription

  24. Купить диплом быстро и качественно
    купить диплом специалиста diplom-msk.ru .

  25. Henryfet表示:

    amoxicillin buy canada: rexall pharmacy amoxicillin 500mg – where can i buy amoxicillin over the counter uk

  26. Henryfet表示:

    amoxicillin medicine over the counter: where can i get amoxicillin 500 mg – amoxicillin 500mg pill

  27. Billyved表示:

    http://doxycyclinea.online/# buy cheap doxycycline

  28. Richardorele表示:

    by prednisone w not prescription: prednisone cost canada – prednisone 2.5 tablet

  29. MarvinRoave表示:

    where can i buy amoxicillin over the counter uk amoxicillin brand name cost of amoxicillin

  30. MarvinRoave表示:

    prednisone 20mg prednisone canada prices medicine prednisone 5mg

發佈留言

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