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

  1. Peterhew表示:

    reputable mexican pharmacies online: best online pharmacies in mexico – reputable mexican pharmacies online

  2. Hi there, just became alert to your blog through Google, and found that it’s truly informative. I?m going to watch out for brussels. I will appreciate if you continue this in future. A lot of people will be benefited from your writing. Cheers!

  3. JeremyPsymn表示:

    https://easyrxcanada.online/# safe canadian pharmacy

  4. TK88表示:

    Greetings, I think your blog could be having browser compatibility problems. When I look at your blog in Safari, it looks fine however when opening in Internet Explorer, it’s got some overlapping issues. I just wanted to give you a quick heads up! Other than that, great blog!

  5. Peterhew表示:

    canadian king pharmacy: canada pharmacy 24h – pharmacy canadian

  6. my homepage … Find top-rated certified Daycares in your area; http://theflatsat540apexnc.com,

  7. Here is my blog post; Daycare Near Me By State (seledtequity.com)

  8. This is a topic which is close to my heart… Take care! Where are your contact details though?

  9. Here is my webpage – Daycare Near Me By State (http://pachecocpa.com/)

  10. Peterhew表示:

    cheapest online pharmacy india: mail order pharmacy india – Online medicine order

  11. My web site :: Find top-rated certified Daycares in your area – http://wannsworld.com

  12. Hermanswoff表示:

    northwest canadian pharmacy: canadian pharmacy 365 – canadian discount pharmacy

  13. Robertengep表示:

    https://easyrxindia.shop/# п»їlegitimate online pharmacies india

  14. Robertengep表示:

    https://easyrxindia.shop/# pharmacy website india

  15. AnthonyPress表示:

    online canadian drugstore canada pharmacy online canadian pharmacy store

  16. Excellent article. I absolutely love this site. Stick with it!

  17. Here is my web site – Daycare Near Me

  18. Eugeneaftek表示:

    Tiny shards of plastic are increasingly infiltrating our brains, study says
    порно групповое жесток
    Human brain samples collected at autopsy in early 2024 contained more tiny shards of plastic than samples collected eight years prior, according to a preprint posted online in May. A preprint is a study which has not yet been peer-reviewed and published in a journal.

    “The concentrations we saw in the brain tissue of normal individuals, who had an average age of around 45 or 50 years old, were 4,800 micrograms per gram, or 0.5% by weight,” said lead study author Matthew Campen, a regents’ professor of pharmaceutical sciences at the University of New Mexico in Albuquerque.
    “Compared to autopsy brain samples from 2016, that’s about 50% higher,” Campen said. “That would mean that our brains today are 99.5% brain and the rest is plastic.”

    That increase, however, only shows exposure and does not provide information about brain damage, said Phoebe Stapleton, an associate professor of pharmacology and toxicology at Rutgers University in Piscataway, New Jersey, who was not involved in the preprint.

    “It is unclear if, in life, these particles are fluid, entering and leaving the brain, or if they collect in neurological tissues and promote disease,” she said in an email. “Further research is needed to understand how the particles may be interacting with the cells and if this has a toxicological consequence.”

    The brain samples contained 7% to 30% more tiny shards of plastic than samples from the cadavers’ kidneys and liver, according to the preprint.

    “Studies have found these plastics in the human heart, the great blood vessels, the lungs, the liver, the testes, the gastrointestinal tract and the placenta,” said pediatrician and biology professor Dr. Philip Landrigan, director of the Program for Global Public Health and the Common Good and the Global Observatory on Planetary Health at Boston College.

    “It’s important not to scare the hell out of people, because the science in this space is still evolving, and nobody in the year 2024 is going to live without plastic,” said Landrigan, who was not involved with the preprint.

  19. AnthonyPress表示:

    canadian pharmacy victoza canadadrugpharmacy com canadian online drugs

發佈留言

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