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

  1. Just wish to say your article is as amazing. The clarity in your put up is simply cool and that i can think you are a professional in this subject. Fine with your permission let me to take hold of your feed to keep up to date with forthcoming post. Thank you 1,000,000 and please carry on the rewarding work.

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

  3. Quite a few scam websites could also be used for this scam.

  4. I?ll right away snatch your rss as I can not find your email subscription hyperlink or e-newsletter service. Do you have any? Kindly permit me recognize in order that I could subscribe. Thanks.

  5. I like what you guys are up too. This kind of clever work and reporting! Keep up the amazing works guys I’ve included you guys to blogroll.

  6. In October 2012, QBE and California agreed to a charge reduction for lender-placed insurance, with an average savings to policyholders of $577 yearly.

  7. Which means work is now not tied to particular places of work at a 9 to 5 job, but will be accomplished wherever because you’ve gotten the ability to speak with one or teams of individuals on a big scale.

  8. One other issue is when you are in a predicament where you don’t have a co-signer then you may want to try to wear out all of your financial aid options. You could find many grants and other scholarships or grants that will ensure that you get funds to support with education expenses. Thanks for the post.

  9. 6231 株価表示:

    When the regiment was disbanded, Rev.

  10. AGCO Corporation Design, assembly and marketing of Challenger track tractors Though founded as an agricultural gear manufacturer, Caterpillar exited the enterprise with this sale.

  11. In 2023, the Universal Medical health insurance Act (UHA) was signed into regulation, mandating that every one residents have a minimum of a minimum stage of medical insurance coverage, from the NHIF or from different approved providers.

  12. Institutional buyers include business banks, central banks, credit unions, government-linked companies, insurers, pension funds, sovereign wealth funds, charities, hedge funds, actual estate investment trusts, investment advisors, endowments, and mutual funds.

  13. Robertleare表示:

    Ang pagpili ng partikular na uri ng account depende sa mga indibidwal na pangangailangan, mga layunin sa pananalapi at mga kagustuhan ng customer. Maraming German bank ang nag-aalok ng payo para matulungan kang pumili ng tamang uri ng account.
    Sheyla
    Ginagamit para sa mga pagpapatakbo ng negosyo , pagbabayad ng mga bill, payroll ng empleyado at iba pang gastusin ng kumpanya.
    No income tax on cryptocurrency income
    Specific method of shareholder notification or declaration

    assess risks and make a timely plan of measures to minimize or eliminate them;
    Crypto Licence in Bulgaria
    The Commercial Company Act is the main piece of legislation governing companies in Portugal. Various types of entities can be incorporated, management can be implemented, and shareholder rights can be enforced.
    Benefits of a Bermuda Digital Asset Business License
    The clarification of the tax regime for cryptocurrency transactions and the possible introduction of favourable tax rates for startups and technology companies contribute to the creation of a competitive economic environment.

  14. Hello there I am so happy I found your site, I really found you by mistake, while I was searching on Aol for something else, Nonetheless I am here now and would just like to say thanks a lot for a incredible post and a all round thrilling blog (I also love the theme/design), I don’t have time to go through it all at the minute but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read more, Please do keep up the great job.

  15. Keithexeld表示:

    The ghost town that has stood empty for more than a century
    жесткое русское порно
    There’s a large and very dignified school in Kayakoy. There are narrow streets, lined with houses, that wend and rise up both sides of a steep valley. There’s an ancient fountain in the middle of the town. And there are churches, one with million-dollar hilltop views over the blue Aegean.

    But, for most of the past 100 years, there have been no people.

    Kayakoy, in southwestern Turkey’s Mugla Province, is a true ghost town. Abandoned by its occupants and haunted by the past. It’s a monument, frozen in time – a physical reminder of darker times in Turkey.

    With hillsides dotted by countless crumbling buildings slowly being swallowed by greenery, and endless views into vanished lives, it’s also a fascinating and starkly beautiful place to visit. In summer, under clear skies and blazing suns, it’s eerie enough. Even more so in cooler seasons, wreathed in mountain or sea mists.
    Just over a century ago, Kayakoy, or Levissi as it was known, was a bustling town of at least 10,000 Greek Orthodox Christians, many of whom were craftspeople who lived peacefully alongside the region’s Muslim Turkish farmers. But in the upheaval surrounding Turkey’s emergence as an independent republic, their simple lives were torn apart.

    Tensions with neighboring Greece after the Greco-Turk war ended in 1922 led to both countries ejecting people with ties to the other. For Kayakoy, that meant a forced population exchange with Muslim Turks living in Kavala, in what is now the Greek region of Macedonia and Thrace.

    But the newly arrived Muslims were reputedly less than happy with their new home, swiftly moving on and leaving Kayakoy to fall to ruin.

  16. Feel free to surf to my page – high stakes Poker

  17. My web site – Find top-rated certified Daycares in your area (https://anadoluyatirim.com.tr/)

  18. Thanks a lot for sharing this with all of us you really know what you’re talking about! Bookmarked. Please also visit my web site =). We could have a link exchange arrangement between us!

  19. winbox88表示:

    You need to be a part of a contest for one of the finest websites on the internet. I most certainly will highly recommend this blog!

  20. 抜け毛量表示:

    Before entering into the agreement, ensure that the product or commodity is of standard quality.

  21. Despite this, Jobs named one of his early Apple computers after her, the Lisa I and Lisa II.

  22. You may operate upon the energy trading tips, you obtain, but it is always precious to keep updated.

  23. Its such as you learn my thoughts! You seem to grasp so much about this, such as you wrote the e book in it or something. I feel that you can do with a few p.c. to force the message home a little bit, however instead of that, this is fantastic blog. A great read. I’ll definitely be back.

  24. On June 1, 2022, following a trial in Fairfax County, Va.

發佈留言

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