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

  1. Gabrielwaimb表示:

    пин ап 634 https://pinupaz.bid/# pinup azerbaycan
    пин ап казахстан

  2. Gabrielwaimb表示:

    пин ап казино вход https://pinupturkey.pro/# pin up casino guncel giris
    pin up kz

  3. Santopax表示:

    https://pinupturkey.pro/# pin up casino giris

  4. ForrestVal表示:

    pin up: пин ап казино – pin up казино

  5. Gabrielwaimb表示:

    pin up kz https://pinupru.site/# пин ап вход
    пин ап казино вход

  6. Armandoknot表示:

    Свежие анекдоты.

  7. Gabrielwaimb表示:

    пинап казино https://pinupturkey.pro/# pin-up casino
    пинап казино

  8. BrianThern表示:

    Hong Kong plans to install thousands of surveillance cameras. Critics say it’s more proof the city is moving closer to China
    адвокат бракоразводный

    Glance up while strolling through parts of downtown Hong Kong and, chances are, you’ll notice the glassy black lens of a surveillance camera trained on the city’s crowded streets.

    And that sight will become more common in the coming years, as the city’s police pursue an ambitious campaign to install thousands of cameras to elevate their surveillance capabilities.

    Though it consistently ranks among the world’s safest big cities, police in the Asian financial hub say the new cameras are needed to fight crime – and have raised the possibility of equipping them with powerful facial recognition and artificial intelligence tools.

    That’s sparked alarm among some experts who see it as taking Hong Kong one step closer to the pervasive surveillance systems of mainland China, warning of the technology’s repressive potential.

    Hong Kong police had previously set a target of installing 2,000 new surveillance cameras this year, and potentially more than that each subsequent year. The force plans to eventually introduce facial recognition to these cameras, security chief Chris Tang told local media in July – adding that police could use AI in the future to track down suspects.
    In a statement to CNN, the Hong Kong Police Force said it was studying how police in other countries use surveillance cameras, including how they use AI. But it’s not clear how many of the new cameras may have facial recognition capabilities, or whether there’s a timeline for when the tech will be introduced.

    Tang and the Hong Kong police have repeatedly pointed to other jurisdictions, including Western democracies, that also make wide use of surveillance cameras for law enforcement. For instance, Singapore has 90,000 cameras and the United Kingdom has more than seven million, Tang told local newspaper Sing Tao Daily in June.

    While some of those places, like the UK, have started using facial recognition cameras, experts say these early experiments have highlighted the need for careful regulation and privacy protections. Hong Kong police told CNN they would “comply with relevant laws” and follow strong internal guidelines – but haven’t elaborated in depth on what that would look like.

  9. DonaldRer表示:

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

  10. Transform your wardrobe with elegant pieces from alice and olivia clothing.

  11. Robertdef表示:

    pin up aviator pin up casino giris pin-up casino

  12. Santopax表示:

    http://pinupaz.bid/# pin up az

  13. ForrestVal表示:

    pinup azerbaycan: pin-up casino giris – pin up 306

  14. Robertdef表示:

    пин ап вход pin up зеркало пин ап

  15. ForrestVal表示:

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

  16. Daycare表示:

    Feel free to surf to my web-site … Daycare

  17. Gabrielwaimb表示:

    pin up казино https://pinupturkey.pro/# pin-up casino
    пин ап

  18. Тут делают продвижение разработка медицинских сайтов создание сайтов для клиники

  19. DonaldRer表示:

    pin-up casino giris: pin up az – pinup az

  20. Gabrielwaimb表示:

    пин ап 634 https://pinupru.site/# пин ап
    pin up

  21. DonaldRer表示:

    pin up казино: пин ап казино вход – пин ап 634

  22. Robertdef表示:

    pin-up casino giris pin up casino guncel giris pin up

  23. ForrestVal表示:

    пин ап 634: пин ап кз – пин ап казино онлайн

  24. DonaldRer表示:

    пин ап: pin up – pin up казино

發佈留言

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