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

  1. Hello, I think yohr blkg might bbe having browser compatibility issues.
    Wheen I lok aat your website in Ie, iit look fine but when openingg in Internhet Explorer, it hhas some
    overlapping. I jujst waanted tto give youu a quick heads up!
    Othher hen that, excellent blog!

  2. Great article. I am dealing with some of these issues as well..

  3. 2019 Annual Report (PDF) (Report).

  4. JoshuaDen表示:

    The man turning jet planes into cool houses
    жесткое групповое порно
    Wasilla, south central Alaska. Home to bears, lakes, mountains and a flight school that’s fast becoming a private aviation wonderland.

    At FLY8MA Pilot Lodge, you can opt for a scenic flight tour with glacier views, take the controls for a flying lesson, or go all in and get your pilot training.

    When night falls over the broad vistas of the US state they call the Last Frontier, you can then climb the steps to two unique accommodation experiences: a converted McDonnell Douglas DC-6 airplane and the newest arrival, a McDonnell Douglas DC-9 – still with its DHL livery.

    The fast-developing site is an ongoing project by FLY8MA founder Jon Kotwicki, who previously owned a flight school in Florida, before working as a commercial pilot and eventually ending up in Alaska.

    Flying for the airlines “pays good money and everything, but it’s a very boring job,” he says. “Driving Uber is more interesting because you could talk to your passengers.”

    Having fallen in love with the south central region on a vacation spent hiking, fishing and spotting bears and grizzlies, he chose it as a spot where he and his team – and his trusty Pomeranian dog Foxtrot – could “buy a lot of property and perhaps develop our own airport and run our own show.”

  5. 飲みかけ表示:

    After being extradited from the Bahamas, Bankman-Fried was released on a $250 million bond and ordered to remain under house arrest at his parents’ home in Palo Alto, California.

  6. PokerTube表示:

    Here is my website :: PokerTube

  7. Great post here. One thing I would really like to say is that most professional job areas consider the Bachelor’s Degree just as the entry level requirement for an online diploma. While Associate Certification are a great way to begin with, completing your current Bachelors starts up many good opportunities to various jobs, there are numerous online Bachelor Diploma Programs available from institutions like The University of Phoenix, Intercontinental University Online and Kaplan. Another concern is that many brick and mortar institutions present Online editions of their diplomas but often for a substantially higher price than the companies that specialize in online college diploma plans.

  8. Buddy表示:

    my web-site – Find top-rated certified Daycares in your area (Buddy)

  9. Frankguara表示:

    The man turning jet planes into cool houses
    жесткий анальный секс
    Wasilla, south central Alaska. Home to bears, lakes, mountains and a flight school that’s fast becoming a private aviation wonderland.

    At FLY8MA Pilot Lodge, you can opt for a scenic flight tour with glacier views, take the controls for a flying lesson, or go all in and get your pilot training.

    When night falls over the broad vistas of the US state they call the Last Frontier, you can then climb the steps to two unique accommodation experiences: a converted McDonnell Douglas DC-6 airplane and the newest arrival, a McDonnell Douglas DC-9 – still with its DHL livery.

    The fast-developing site is an ongoing project by FLY8MA founder Jon Kotwicki, who previously owned a flight school in Florida, before working as a commercial pilot and eventually ending up in Alaska.

    Flying for the airlines “pays good money and everything, but it’s a very boring job,” he says. “Driving Uber is more interesting because you could talk to your passengers.”

    Having fallen in love with the south central region on a vacation spent hiking, fishing and spotting bears and grizzlies, he chose it as a spot where he and his team – and his trusty Pomeranian dog Foxtrot – could “buy a lot of property and perhaps develop our own airport and run our own show.”

  10. Richardsudge表示:

    The man turning jet planes into cool houses
    порно анальный секс
    Wasilla, south central Alaska. Home to bears, lakes, mountains and a flight school that’s fast becoming a private aviation wonderland.

    At FLY8MA Pilot Lodge, you can opt for a scenic flight tour with glacier views, take the controls for a flying lesson, or go all in and get your pilot training.

    When night falls over the broad vistas of the US state they call the Last Frontier, you can then climb the steps to two unique accommodation experiences: a converted McDonnell Douglas DC-6 airplane and the newest arrival, a McDonnell Douglas DC-9 – still with its DHL livery.

    The fast-developing site is an ongoing project by FLY8MA founder Jon Kotwicki, who previously owned a flight school in Florida, before working as a commercial pilot and eventually ending up in Alaska.

    Flying for the airlines “pays good money and everything, but it’s a very boring job,” he says. “Driving Uber is more interesting because you could talk to your passengers.”

    Having fallen in love with the south central region on a vacation spent hiking, fishing and spotting bears and grizzlies, he chose it as a spot where he and his team – and his trusty Pomeranian dog Foxtrot – could “buy a lot of property and perhaps develop our own airport and run our own show.”

  11. Darrelherse表示:

    slot kumar siteleri: yeni slot siteleri – slot siteleri 2024

  12. One thing I want to say is before acquiring more laptop or computer memory, look into the machine into which it can be installed. If your machine is definitely running Windows XP, for instance, the memory limit is 3.25GB. Putting in over this would just constitute some sort of waste. Be sure that one’s motherboard can handle an upgrade quantity, as well. Great blog post.

  13. Hello, you used to write wonderful, but the last several posts have been kinda boring? I miss your great writings. Past several posts are just a bit out of track! come on!

  14. KevinSounc表示:

    http://slotsiteleri.bid/# 2024 en iyi slot siteleri

  15. Darrelherse表示:

    slot casino siteleri: casino slot siteleri – slot bahis siteleri

  16. Also visit my homepage … Daycare Near Me

  17. JesusLealt表示:

    slot siteleri 2024: en guvenilir slot siteleri – deneme veren slot siteleri

  18. It?s actually a nice and helpful piece of info. I am glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.

  19. Darrelherse表示:

    slot siteleri 2024: 2024 en iyi slot siteleri – slot oyun siteleri

  20. Very good post, thanks a lot for sharing. Do you happen to have an RSS feed I can subscribe to?

  21. I appreciate, cause I found exactly what I was looking for. You’ve ended my four day long hunt! God Bless you man. Have a nice day. Bye

  22. Look at my homepage: Daycares By Category (butlerstudyabroad.org)

發佈留言

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