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

  1. Josephnor表示:

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

  2. St Catherine’s Church Irchester, Northamptonshire 1924 Three-gentle window in St Catherine’s North Nave depicts the life and martyrdom of St Catherine.

  3. Justinlib表示:

    https://slottr.top/# slot tr online

  4. EdwardApest表示:

    http://casinositeleri2025.pro/# gГјvenilir bahis siteleri 2025

  5. Justinlib表示:

    https://casinositeleri2025.pro/# bГјtГјn oyun siteleri

  6. TravisFlasp表示:

    kaГ§ak siteler: yeni aГ§Д±lan bahis siteleri – bonus veren site

  7. RobertPex表示:

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

  8. RobertPex表示:

    slot oyunlar? puf noktalar? az parayla cok kazandiran slot oyunlar? slot tr online

  9. EdwardApest表示:

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

  10. Josephnor表示:

    пин ап: pinup 2025 – пин ап

  11. RobertPex表示:

    pinup 2025 пин ап вход пин ап казино

  12. Justinlib表示:

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

  13. Justinlib表示:

    http://slottr.top/# slot tr online

  14. JamesAreft表示:

    They fell in love three decades ago. Now they pilot planes together
    Aave

    On their first flight together, Joel Atkinson and Shelley Atkinson couldn’t contain their excitement. They enthused to the flight attendants. They posed for photos. They told passengers via a pre-flight announcement.

    “We made a big deal about it,” Joel tells CNN Travel.

    Then, right before take off, Joel and Shelley sat side by side in the flight deck, just the two of them. They’d come full circle, and were about to embark on an exciting new chapter.

    “It felt amazing,” Shelley tells CNN Travel.

    “As we prepared to take off, I was giddy, euphoric,” says Joel.

    Joel and Shelley met as twentysomethings flying jets in the US Air Force. They became fast friends, then, over time, fell in love.

    Today, they’ve been married for 27 years and counting. They’ve brought up two kids together. And now they’re both pilots for Southwest Airlines. They regularly fly together, with Joel as captain and Shelley as first officer.

    The couple say working together is “amazing.” They treat layovers as “date nights.” They learn from one another’s respective “wisdom and judgment.”

    And no, they don’t argue mid-flight.

    “People ask us, how does it work, flying together?” says Joel. “We know a few pilot couples and some of them fly together, some of them don’t. I’ve heard people say, ‘Oh I could never fly with my wife or my husband.’”

    For Joel and Shelley, working together is seamless – a joy that comes easily to them both.

    “We’re best friends,” says Shelley.

    “There’s just that unspoken bond,” says Joel.

  15. Josephnor表示:

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

  16. RobertPex表示:

    tГјrkiye yasal bahis siteleri bahis oyun siteleri yasal oyun siteleri

  17. Justinlib表示:

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

  18. EdwardApest表示:

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

  19. Justinlib表示:

    http://slottr.top/# az parayla cok kazandiran slot oyunlar?

  20. Josephnor表示:

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

  21. Anthonyacawl表示:

    How to survive a bear attack – or better yet, avoid one altogether
    Uniswap

    You’re out for a hike, reveling in glorious nature. Suddenly, you spot a bear. And the bear has spotted you, too. Would you know what to do next?

    Beth Pratt sure would.

    She was once on the Old Gardiner Road Trail in Yellowstone National Park, enjoying her run in wild nature. Her reverie came to an end when she came upon a grizzly bear eating flowers.

    “I stopped. It stood on its hind legs and looked at me. I knew that wasn’t a threatening gesture,” she told CNN Travel. “I’m not kidding, it waved its paw at me as if to say, ‘just go on your way,’ and went back to eating.”

    “And I walked slowly away and put some distance between us, and the encounter ended fine.”

    When it comes to dealing with bears, Pratt does have a thing or two on almost all the rest of us, though.

    She is the California regional executive director for the National Wildlife Federation, a job she’s had for more than 10 years. She worked in Yellowstone for several years – and once saw nine grizzlies in one day there.
    Finally, she lives on the border of Yosemite National Park, and bears will pass through her yard, including this one seen in the footage above in late September 2021.

    You can hear the enthusiasm in Pratt’s voice as she shares her bear bona fides and advice to make sure bear/human encounters are delightful, not dangerous.

    “A wild bear is a beautiful sight to see. It’s incredible to see them in the wild. I never had a bad experience with bears. What I try to get people to feel is respect, not fear, for bears. The animal usually wants to avoid the encounters.”

  22. Justinlib表示:

    https://slottr.top/# az parayla cok kazandiran slot oyunlar?

發佈留言

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