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

42,921 Responses

  1. JosephVed表示:

    фиброцементный сайдинг для фасада купить купить сайдинг для обшивки дома

  2. Michaelcot表示:

    казино регистрации сайт твин казино

  3. Wow, fantastic weblog structure! How long have you been blogging for? you make blogging glance easy. The whole glance of your website is great, as neatly as the content material!

  4. I think your blog is getting more and more visitors.`”:,,

  5. Williamdut表示:

    купить диплом старого образца https://www.6landik-diploms.com/

  6. JosephVed表示:

    сайдинг для отделки дома купить https://kanoner.com

  7. As being a Newbie, We’re permanently exploring online for articles which can be of help to me. Many thanks

  8. Williamdut表示:

    купить диплом нового образца http://6landik-diploms.com/

  9. Youre so cool! I dont suppose Ive read anything like this before. So nice to find somebody with original applying for grants this subject. realy thank you for beginning this up. this amazing site is something that is required on the web, an individual if we do originality. valuable job for bringing new stuff towards the internet!

  10. nicotine can really make you an addict, stay away from cigarettes in the first place::

  11. Jamesbok表示:

    купить диплом в иваново
    сайт

  12. Ganhe no cassino mais confiavel Blaze

  13. JamesTeact表示:

    купить диплом в димитровграде
    на сайте

  14. Отличный сайт! Всем рекомендую! Тут Вы можете найтилучшие брокеры для скальпинга

  15. Impressive piece of content! The way we wish had that understanding. I’m hoping to study a lot more on your side. You will find you might amazing details combined with idea. I’m sure tremendously contented with that info.

  16. There may be jacksonville seo expert an easy method in making income without having being employed on your own ill along with devoid of the your complete moment. You can use the facility belonging to the Internet for you to earn more money and possess plenty of time to really love this website. Despite exactly how bit of you know about the World wide web or perhaps how much company encounter you’ve, you’ll be able to utilize a variety of fantastic prospects to generate capital suitable during your computer.

  17. Jamesbok表示:

    купить диплом специалиста
    подробнее

  18. JamesTeact表示:

    купить диплом в москве
    по ссылке

  19. An impressive share, I simply given this onto a colleague who was simply doing small analysis with this. And the man in reality bought me breakfast because I discovered it for him.. smile. So permit me to reword that: Thnx with the treat! But yeah Thnkx for spending plenty of time to go over this, I am strongly concerning this and love reading much more about this topic. If at all possible, as you become expertise, do you mind updating your website with an increase of details? It really is extremely helpful for me. Huge thumb up because of this post!

  20. I am sure some of this post is going to blow people’s brains, though this is only the tip of the iceberg.

  21. I was questioning occasion you ever considered altering layout , design from the website? Its really correctly created; I enjoy what youve obtained to mention. But possibly it is possible to small far more with respect to content so males could interact with it greater. Youve obtained a terrible total lots of text for only getting a single or two images. Maybe you’ll be capable of area out greater?

  22. We are a group of volunteers and starting a new project in our neighborhood. Your blog provided us with valuable information to help us get started|.You have done a great job!

  23. The most important L . a . Weight reduction plan is usually an sensibly priced and flexible going on a diet service planned for folks who are expecting to in losing pounds and consequently essentially maintain a more fit diet. weight loss

  24. JerryHeque表示:

    заказать настоящий корень мимозы хостилис из мексики https://travelservic.ru

  25. Отличный сайт! Всем рекомендую! Тут Вы можете найтиброкер для скальпинга

  26. GeraldBof表示:

    bicrypto – crypto exchange software. White label, open-source exchange solution with a focus on a super-fast, pixel-perfect interface and robust security. High-performance platform with a robust internal architecture. Leverages the capabilities of Nuxt3 to create a cutting-edge user interface.

  27. This would be the proper blog for anybody who wishes to learn about this topic. You know so much its almost challenging to argue along (not too I just would want…HaHa). You definitely put a whole new spin for a topic thats been revealed for several years. Wonderful stuff, just wonderful!

  28. I simply could not depart your site before suggesting that I really loved the usual information an individual supply in your visitors? Is gonna be again frequently to inspect new posts.

  29. GeraldBof表示:

    https://bicrypto.exchange – crypto exchange software. White label, open-source exchange solution with a focus on a super-fast, pixel-perfect interface and robust security. High-performance platform with a robust internal architecture. Leverages the capabilities of Nuxt3 to create a cutting-edge user interface.

發佈留言

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