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

  1. MichaelSom表示:

    can i buy clomid for sale: can i order generic clomid without insurance – can i buy clomid

  2. Timothyket表示:

    prednisone 20mg cheap: prednisone tablets india – prednisone 20 mg generic

  3. Antoniobus表示:

    https://ciprofloxacin.life/# buy ciprofloxacin

  4. Michaeltaisa表示:

    http://amoxil.icu/# amoxil pharmacy

  5. Antoniobus表示:

    https://ciprofloxacin.life/# buy ciprofloxacin

  6. MichaelWence表示:

    order cheap clomid can you buy generic clomid – can you buy generic clomid tablets

  7. Michaeltaisa表示:

    https://amoxil.icu/# amoxicillin 500

  8. MichaelWence表示:

    how can i get cheap clomid for sale can i order cheap clomid for sale – cost cheap clomid price

  9. MichaelWence表示:

    can you buy cheap clomid now get clomid online – where to buy cheap clomid without rx

  10. MichaelWence表示:

    can i buy clomid without prescription clomid – how to get cheap clomid pills

  11. MichaelWence表示:

    where to buy clomid without a prescription cost of cheap clomid no prescription – can i purchase cheap clomid without insurance

  12. MichaelWence表示:

    how to buy clomid no prescription cost clomid without a prescription – can you get clomid without a prescription

  13. Danielnek表示:

    https://amoxil.icu/# cost of amoxicillin prescription

  14. MichaelSom表示:

    clomid cheap: cost clomid no prescription – can i buy clomid no prescription

  15. Timothyket表示:

    cheap generic prednisone: prednisone pak – canada buy prednisone online

  16. MichaelWence表示:

    can i purchase generic clomid pill get cheap clomid without dr prescription – buy generic clomid prices

  17. Danielnek表示:

    http://amoxil.icu/# amoxicillin without rx

  18. Michaeltaisa表示:

    https://amoxil.icu/# amoxicillin 500mg no prescription

  19. Michaeltaisa表示:

    https://amoxil.icu/# buying amoxicillin in mexico

  20. Michaeltaisa表示:

    http://amoxil.icu/# amoxicillin online canada

  21. MichaelWence表示:

    how to get cheap clomid pills buying clomid tablets – cost clomid without a prescription

  22. Danielnek表示:

    https://amoxil.icu/# amoxicillin 500mg no prescription

  23. Danielnek表示:

    https://amoxil.icu/# buy amoxicillin

  24. MichaelWence表示:

    buy generic clomid cost of cheap clomid tablets – how to buy cheap clomid without prescription

  25. MichaelSom表示:

    can i buy generic clomid without prescription: where to get clomid no prescription – where can i get generic clomid without a prescription

  26. Timothyket表示:

    prednisone 30 mg: prednisone no rx – how can i order prednisone

  27. Danielnek表示:

    https://amoxil.icu/# prescription for amoxicillin

  28. Michaeltaisa表示:

    https://amoxil.icu/# amoxicillin 500mg price in canada

  29. MichaelWence表示:

    how can i get generic clomid without insurance where buy clomid no prescription – can i purchase generic clomid pills

  30. MichaelWence表示:

    where can i buy clomid for sale can i purchase cheap clomid without a prescription – cost of clomid without prescription

發佈留言

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