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

41,733 Responses

  1. TomasRop表示:

    farmacie online sicure: Cialis generico controindicazioni – acquistare farmaci senza ricetta

  2. TomasRop表示:

    migliori farmacie online 2024: kamagra – top farmacia online

  3. Отличный сайт! Всем рекомендую!справочник

  4. RandyLunda表示:

    farmacie online sicure: Cialis generico recensioni – farmacie online autorizzate elenco

  5. Strong blog. I acquired various nice information. I?ve been keeping an eye fixed on this technology for some time. It?utes attention-grabbing the way it retains completely different, however a number of of the primary parts stay constant. have you ever observed plenty amendment since Search engines created their own latest purchase within the field?

  6. RandyLunda表示:

    farmacie online sicure: Cialis generico 20 mg 8 compresse prezzo – comprare farmaci online all’estero

  7. Отличный сайт! Всем рекомендую!каталог сайтов

  8. I got what you mean , regards for posting .Woh I am pleased to find this website through google.

  9. I blog quite often and I truly appreciate your information. Your article has really peaked my interest. I’m going to bookmark your blog and keep checking for new information about once a week. I opted in for your Feed as well.

  10. Woah! I’m really loving the template/theme of this website. It’s simple, yet effective. A lot of times it’s hard to get that “perfect balance” between user friendliness and visual appeal. I must say that you’ve done a very good job with this. Additionally, the blog loads super fast for me on Safari. Exceptional Blog!

  11. of course like your web-site however you need to check the
    spelling on several of your posts. Several of them are rife with spelling issues and
    I to find it very troublesome to tell the reality nevertheless I’ll certainly come
    back again.

    Check out my webpage fan888 เครดิตฟรี

  12. It’s very trouble-free to find out any matter on web as
    compared to textbooks, as I found this piece of writing at this
    site.

    Also visit my web site – online casino philippines

  13. Jerry表示:

    Feel free to visit my web-site: Jerry

  14. LewisTwera表示:

    https://propeciabestprice.pro/# cost propecia online

  15. Matthewchact表示:

    zithromax coupon: can you buy zithromax over the counter in mexico – zithromax online paypal

  16. you’re really a good webmaster. The web site loading speed is amazing. It seems that you are doing any unique trick. Furthermore, The contents are masterwork. you have done a magnificent job on this topic!

  17. magnificent submit, very informative. I’m wondering why the opposite specialists of this sector do not understand this.

    You must proceed your writing. I’m sure,
    you’ve a great readers’ base already!

    Here is my web page is lucky spins casino legit

  18. I’ve learned many important things as a result of your post. I’d also like to say that there will be a situation in which you will apply for a loan and do not need a co-signer such as a Federal government Student Aid Loan. However, if you are getting that loan through a traditional financial institution then you need to be made ready to have a cosigner ready to allow you to. The lenders will certainly base their very own decision over a few components but the greatest will be your credit worthiness. There are some financial institutions that will furthermore look at your work history and make up your mind based on that but in many instances it will be based on on your credit score.

  19. Отличный сайт! Всем рекомендую!рейтинг сайтов

  20. First off I would like to say great blog! I had a quick
    question that I’d like to ask if you do not mind.
    I was curious to find out how you center yourself and clear your head
    prior to writing. I have had trouble clearing my mind in getting my ideas out there.
    I do take pleasure in writing however it just seems like
    the first 10 to 15 minutes tend to be lost simply just trying
    to figure out how to begin. Any suggestions or tips? Thanks!

    Also visit my site … ae bet เครดิตฟรี

  21. Matthewchact表示:

    generic zithromax azithromycin: zithromax 500 mg – zithromax online paypal

  22. Pretty great post. I just stumbled upon your weblog and wished to mention that I have really enjoyed surfing around your blog posts. After all I?ll be subscribing on your feed and I’m hoping you write again soon!

  23. Matthewchact表示:

    20 mg prednisone: buy prednisone online india – can i purchase prednisone without a prescription

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

  25. LewisTwera表示:

    https://propeciabestprice.pro/# order generic propecia without rx

發佈留言

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