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

48,230 Responses

  1. Justinlib表示:

    http://pinup2025.com/# пин ап зеркало

  2. EdwardApest表示:

    http://pinup2025.com/# пин ап казино

  3. гибкий электрокарниз привод гибкий электрокарниз привод .

  4. klima allianz deutschland [url=https://wettervorhersage-deutschland.com/]klima allianz deutschland[/url] .

  5. накрутить подписчиков твиттер [url=https://nakrutkamedia14.com]накрутить подписчиков твиттер[/url] .

  6. рулонные шторы с электроприводом цена [url=http://www.rulonnye-shtory-s-elektroprivodom499.ru]рулонные шторы с электроприводом цена[/url] .

  7. автоматические гардины для штор автоматические гардины для штор .

  8. Josephnor表示:

    пин ап зеркало: пин ап казино – пин ап казино

  9. Justinlib表示:

    http://pinup2025.com/# пин ап зеркало

  10. Feel free to surf to my website :: Daycare Near Me (lovejoylinks.com)

  11. Josephnor表示:

    slot tr online: slot oyunlar? puf noktalar? – slot oyunlar? puf noktalar?

  12. Josephnor表示:

    pinup 2025: пин ап зеркало – pinup 2025

  13. EdwardApest表示:

    http://slottr.top/# slot oyunlar?

  14. Justinlib表示:

    http://slottr.top/# slot oyunlar?

  15. RobertPex表示:

    internet kumar siteleri casino gГјvenilir siteler gГјvenli siteler

  16. TravisFlasp表示:

    pinup 2025: пинап казино – pinup 2025

  17. EdwardApest表示:

    https://slottr.top/# en cok kazand?ran slot oyunlar?

  18. Robertwaymn表示:

    Bug-bitten oolong? The secret behind Taiwan’s rare honey-flavored tea — and where to enjoy it
    Ethena

    As the leaves rustle atop the hills in Nantou, Taiwan’s largest tea-producing area, the farm suddenly comes alive, millions of tiny green bugs hopping into the air.

    While many farmers might frown at the sight of these pests munching on their crops, Lee Ming-cheng, a third-generation tea farmer and maker, can’t hide the broad smile on his sun-kissed face.

    This “green insect fog,” as locals call it, is a sign they’ll have a good harvest of Gui Fei Oolong (also known as Honey Flavor Dong Ding Oolong or Concubine Oolong), a special tea that’s prized for offering a hint of honey flavor.

    And it’s these endemic insects, called Jacobiasca formosana, or tea jassids, that are to thank for it.

    When the jassids feed, the leaves go into defensive mode and produce a sweetened hormone that tastes and smells like honey, creating one of the world’s most intriguing teas: mixiang cha, or honey-fragrance tea.

    The bug-bitten leaves are oxidized and roasted to create a variety of beverages. There’s mixiang black tea (made with fully oxidized leaves) and oolong teas like Oriental Beauty (partially oxidized and not roasted) and the previously mentioned Concubine Tea (partially oxidized and roasted), to name a few.

    Unlike Taiwan’s ubiquitous bubble tea, mixiang tea is still highly limited and largely off-the-radar. But what was once a hidden gem among serious tea lovers is now starting to gain international attention.

  19. TravisFlasp表示:

    100tl hosgeldin bonusu veren siteler: online casino websites – en iyi canlД± casino siteleri

  20. RobertPex表示:

    пин ап пин ап pinup 2025

  21. Josephnor表示:

    yurt dД±ЕџД± bahis sitesi: gГјvenli siteler – bahis oyun siteleri

發佈留言

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