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

55,753 Responses

  1. JamesGag表示:

    mexican pharmacy online store: mexican pharmacy online order – mexican pharmacy online order

  2. Montebeera表示:

    reliable mexican pharmacies buying from online mexican pharmacy buying from online mexican pharmacy

  3. Joshuaerult表示:

    The crypto industry is advancing. (Just don’t ask it where it’s going.)
    trust wallet
    After a dramatic start to the year, the crypto industry is settling into a new reality — one in which the White House is laying out the red carpet and promising an unprecedented level of support.

    Crypto, a roughly 15-year-old industry that’s largely operated on the fringes of finance, is at a crossroads. For years, it has blamed a hostile regulatory environment for not allowing it to unleash its supposedly revolutionary technology on Americans. Now, though, their favorite bogeyman, Gary Gensler, the Securities and Exchange Commission chief under President Joe Biden, is gone. Crypto cheerleaders have been installed throughout the government.

    The SEC has dropped several enforcement cases against crypto companies and, starting Friday, is hosting a series of public roundtables “to discuss key areas of interest in the regulation of crypto assets.”

    Under President Donald Trump, there’s virtually nothing stopping crypto companies from creating and selling their products.

    At the same time, the same White House’s chaotic trade policy is undermining financial markets’ appetite for risk, leaving bitcoin in limbo, more than 20% off from its record high in January. And while the industry is grateful for all the attention, the White House’s embrace of some of crypto’s less savory aspects, like meme coins, has given serious investors pause.

    Given the enormous potential for the $3 trillion industry in this moment, I checked in with Eswar Prasad, a Cornell University professor of international trade and the author of the 2021 book “The Future of Money,” about the forces disrupting financial technologies.

    Fundamentally, Prasad brings a pragmatist’s view of crypto that is as refreshing as it is rare in a subject area that tends to attract zealots and loudmouths. We spoke over the phone shortly after the first-of-its-kind White House crypto summit earlier this month.

    The following interview has been edited for length and clarity.

    Nightcap: We just saw a pretty wild thing happen with the crypto summit — hard to imagine a scenario like that taking place under any previous administration. What were your takeaways?

    Eswar Prasad: The crypto industry is kissing the ring, and I think it’s getting exactly what it wants from the Trump administration, which is the legitimacy provided by government oversight, coupled with what is almost certain to be quite light touch and non-inclusive regulation.

    And I think we saw many of the major players in the crypto industry essentially using the opportunity to not just thank Trump, but try to make the point, which seemed to resonate with Trump, that this industry can power, in some sense, a resurgence of a certain part of the US economy.

  4. Arthurhow表示:

    http://interpharmonline.com/# canadian pharmacy com
    online canadian pharmacy

  5. Pingidiof表示:

    bitcoin dark web https://github.com/aresonioncq0a7/aresonion – darknet markets onion address

  6. Howardenace表示:

    canadian drug pharmacy: certified canada pharmacy online – canada drugs online review

  7. DonDonrob表示:

    darknet drug market https://github.com/nexusdarkrtv1u/nexusdark – darknet markets onion

  8. Arthurhow表示:

    https://indiamedfast.com/# IndiaMedFast
    canadapharmacyonline

  9. Williszew表示:

    https://interpharmonline.shop/# canadian pharmacy ltd

  10. JamesGag表示:

    buying prescription drugs from india: order medicines online india – india online pharmacy store

  11. Pingidiof表示:

    darknet market list https://github.com/aresonioncq0a7/aresonion – darknet markets 2025

  12. Williszew表示:

    https://interpharmonline.com/# canadian pharmacy price checker

  13. JamesGag表示:

    canadian online drugstore: online canadian pharmacy no prescription – ordering drugs from canada

  14. Arthurhow表示:

    http://interpharmonline.com/# adderall canadian pharmacy
    canadian pharmacy mall

  15. Moemate表示:

    Appreciate you sharing, great article.Much thanks again. Much obliged.

  16. Arthurhow表示:

    https://mexicanpharminter.shop/# mexican drug stores online
    canadian mail order pharmacy

  17. Williszew表示:

    http://mexicanpharminter.com/# reliable mexican pharmacies

  18. Arthurhow表示:

    https://indiamedfast.shop/# order medicines online india
    canadian pharmacy online

  19. Williszew表示:

    https://indiamedfast.shop/# india pharmacy without prescription

  20. Williszew表示:

    https://mexicanpharminter.shop/# mexican drug stores online

  21. JamesGag表示:

    Mexican Pharm Inter: MexicanPharmInter – Mexican Pharm International

發佈留言

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