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

46,483 Responses

  1. Do you mind if I quote a few of your posts as long as I provide credit and sources back to your site? My blog site is in the very same area of interest as yours and my users would definitely benefit from a lot of the information you present here. Please let me know if this okay with you. Cheers!

  2. Hey there would you mind letting me know which webhost you’re using? I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot faster then most. Can you suggest a good web hosting provider at a reasonable price? Thank you, I appreciate it!

  3. Risk management software allows a business to more effectively control risk and to more effectively communicate real-time information about risk with potential customers, shareholders and anyone else who needs to get a good grasp on the “risk facts.” The first step is to establish trust with stakeholders and involve them as legitimate partners in the business.

  4. Ok oui et pas vraiment. Assurément parce que il se peut qu’on rencontre certaines sources qui probablement citent de semblables cote. Non car il ne suffit pas de copier ce qu’on peut lire avec plusieurs site web autres avant de le transposer tant clairement

  5. WayneCoirl表示:

    buying from online mexican pharmacy: mexican rx online – mexican border pharmacies shipping to usa

  6. DominicJaw表示:

    mexico drug stores pharmacies: п»їbest mexican online pharmacies – п»їbest mexican online pharmacies

  7. Nelsonbluck表示:

    medicine in mexico pharmacies: mexican online pharmacies prescription drugs – mexican border pharmacies shipping to usa

  8. Arnoldten表示:

    purple pharmacy mexico price list mexican rx online mexico pharmacies prescription drugs

  9. Wow that was unusual. I just wrote an really long comment but after I clicked submit my comment didn’t show up. Grrrr… well I’m not writing all that over again. Anyhow, just wanted to say wonderful blog!

  10. WayneCoirl表示:

    medication from mexico pharmacy: п»їbest mexican online pharmacies – medication from mexico pharmacy

  11. Nice blog here! Additionally your website so much up fast! What web host are you using? Can I get your affiliate link in your host? I desire my website loaded up as fast as yours lol.

  12. DominicJaw表示:

    п»їbest mexican online pharmacies: best online pharmacies in mexico – buying prescription drugs in mexico online

  13. Arnoldten表示:

    mexican border pharmacies shipping to usa best online pharmacies in mexico mexico pharmacy

  14. Nelsonbluck表示:

    purple pharmacy mexico price list: reputable mexican pharmacies online – medication from mexico pharmacy

  15. Audio began playing when I opened up this internet site, so irritating!

  16. Do you have a spam issue on this site; I also am a blogger, and I was wanting to know your situation; we have developed some nice practices and we are looking to swap techniques with others, why not shoot me an email if interested.

  17. WayneCoirl表示:

    mexican pharmaceuticals online: mexico drug stores pharmacies – purple pharmacy mexico price list

  18. DominicJaw表示:

    buying prescription drugs in mexico: buying prescription drugs in mexico online – reputable mexican pharmacies online

  19. Arnoldten表示:

    buying from online mexican pharmacy buying prescription drugs in mexico medicine in mexico pharmacies

  20. Nelsonbluck表示:

    mexican mail order pharmacies: mexican drugstore online – mexican mail order pharmacies

  21. There’s noticeably a bundle to find out about this. I assume you made certain good points in options also.

  22. WayneCoirl表示:

    mexico drug stores pharmacies: mexican rx online – mexican mail order pharmacies

  23. DominicJaw表示:

    п»їbest mexican online pharmacies: medication from mexico pharmacy – medicine in mexico pharmacies

  24. Arnoldten表示:

    mexico pharmacies prescription drugs purple pharmacy mexico price list medication from mexico pharmacy

  25. Nelsonbluck表示:

    buying from online mexican pharmacy: buying from online mexican pharmacy – mexican mail order pharmacies

  26. I just right now wished to let you know about how exactly significantly I actually value all things you have discussed to help enhance life of individuals on this material. Using your articles, I have long gone by means of merely a beginner to a professional in your community. It is really a homage for your initiatives. Thank you

  27. I am frequently to blogging and i actually appreciate your content regularly. Your content has really peaks my interest. Let me bookmark your internet site and maintain checking for first time information.

  28. Sdtmlj表示:

    buy terazosin 5mg generic – buy tamsulosin 0.2mg online how to buy priligy

  29. WayneCoirl表示:

    mexican drugstore online: medicine in mexico pharmacies – mexico drug stores pharmacies

發佈留言

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