透過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. Jeffreygef表示:

    Arrowheads reveal the presence of a mysterious army in Europe’s oldest battle
    жесткое порно видео
    Today, the lush, green valley surrounding the Tollense River in northeast Germany appears to be a serene place to appreciate nature.

    But to archaeologists, the Tollense Valley is considered Europe’s oldest battlefield.

    An amateur archaeologist first spotted a bone sticking out of the riverbank in 1996.

    A series of ongoing site excavations since 2008 has shown that the thousands of bones and hundreds of weapons preserved by the valley’s undisturbed environment were part of a large-scale battle 3,250 years ago.

    The biggest mysteries that researchers aim to uncover are why the battle occurred and who fought in it. These are questions that they are now one step closer to answering.
    ozens of bronze and flint arrowheads recovered from the Tollense Valley are revealing details about the able-bodied warriors who fought in the Bronze Age battle.

    The research team analyzed and compared the arrowheads, some of which were still embedded in the remains of the fallen. While many of these weapons were locally produced, some bearing different shapes came from a region that now includes modern Bavaria and Moravia.

    The outliers’ presence suggests that a southern army clashed with local tribes in the valley, and researchers suspect the conflict began at a key landmark along the river.

    Back to the future
    Scientists are harnessing the power of artificial intelligence to detect hidden archaeological sites buried below the sand of the sprawling Rub‘ al-Khali desert.

    The desert spans 250,000 square miles (650,000 square kilometers) on the Arabian Peninsula, and its name translates to “the Empty Quarter” in English. To unravel the secrets of the desolate terrain, researchers are combining machine learning with a satellite imagery technique that uses radio waves to spot objects that may be concealed beneath surfaces.

    The technology will be tested in October when excavations assess whether predicted structures are present at the Saruq Al Hadid complex in Dubai, United Arab Emirates.

    Separately, an AI-assisted analysis uncovered a trove of ancient symbols in Peru’s Nazca Desert, nearly doubling the number of known geoglyphs, or stone and gravel arranged into giant shapes that depict animals, humans and geometric designs.

  2. Jamestiz表示:

    Как пайщик кооператива «Бест Вей», я возмущен тем, что происходит вокруг нашего дела. Это чистой воды коррупция, когда органы власти вместо того, чтобы поддерживать проекты, которые работают на благо людей, стремятся уничтожить их ради своей выгоды. Я лично получил жилье через кооператив и знаю десятки людей, которые смогли сделать то же самое. Мы все видим, что следствие ведется с явной предвзятостью. Обвинения сфабрикованы, а в деле фигурируют подставные «потерпевшие». Все это сделано с одной целью – отобрать наши активы и дискредитировать тех, кто честно работал. Но это не просто уничтожение компании, это разрушение жизней тысяч людей, которые верили в этот кооператив. Следственные органы, прокуратура, все, кто причастен к этой грязной игре, рано или поздно будут разоблачены. Мы продолжим бороться за свои права, и я уверен, что правда восторжествует.

  3. HaroldBig表示:

    buy cheap ozempic: Ozempic without insurance – ozempic cost

  4. Ho88表示:

    Having read this I believed it was really enlightening. I appreciate you taking the time and energy to put this short article together. I once again find myself spending a lot of time both reading and posting comments. But so what, it was still worth it.

  5. 오피表示:

    I couldn’t agree more with your conclusions.오피

  6. Jeremypag表示:

    semaglutide tablets rybelsus coupon rybelsus price

  7. Jeremypag表示:

    buy ozempic ozempic generic buy cheap ozempic

  8. RamonRek表示:

    buy semaglutide online: buy semaglutide pills – rybelsus coupon

  9. RobertGed表示:

    betting welcome bonuses: These are special codes for the Melbet platform that provide users with bonuses, such as free bets or spins, without the need to make a deposit.

  10. HaroldBig表示:

    buy cheap ozempic: buy cheap ozempic – buy cheap ozempic

  11. Frankevild表示:

    http://rybelsus.shop/# semaglutide cost

  12. HaroldBig表示:

    buy rybelsus online: rybelsus coupon – rybelsus pill

  13. Frankevild表示:

    https://rybelsus.shop/# buy rybelsus online

  14. BarryDof表示:

    https://rybelsus.shop/# cheapest rybelsus pills

  15. RamonRek表示:

    buy rybelsus online: rybelsus pill – buy semaglutide online

  16. Jeremypag表示:

    buy semaglutide pills semaglutide tablets buy semaglutide online

  17. HaroldBig表示:

    rybelsus cost: buy semaglutide pills – rybelsus cost

  18. HaroldBig表示:

    ozempic cost: buy ozempic – ozempic cost

  19. RamonRek表示:

    buy rybelsus online: cheapest rybelsus pills – semaglutide cost

  20. BarryDof表示:

    http://ozempic.art/# buy cheap ozempic

  21. webcam sex表示:

    Everything is very open with a really clear description of the issues. It was really informative. Your website is useful. Thank you for sharing.

  22. Frankevild表示:

    http://rybelsus.shop/# semaglutide online

  23. Jeremypag表示:

    ozempic coupon Ozempic without insurance buy cheap ozempic

  24. HaroldBig表示:

    rybelsus coupon: rybelsus pill – rybelsus pill

  25. Frankevild表示:

    https://rybelsus.shop/# cheapest rybelsus pills

  26. Armandoknot表示:

    Смешные свежие анекдоты и шутки
    Подними себе настроение

  27. Jeremypag表示:

    buy rybelsus online buy rybelsus online buy semaglutide online

  28. BarryDof表示:

    https://rybelsus.shop/# semaglutide cost

發佈留言

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