透過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...

50,369 Responses

  1. After you’ve learn up on the most recent modifications and filed your tax returns, ensure to keep away from the next mistake on our checklist.

  2. BrettTIT表示:

    Buy Tadalafil 10mg: MaxPillsForMen – Buy Tadalafil 5mg

  3. BrettTIT表示:

    cheapest ed online: fast pills easy – cost of ed meds

  4. Donaldabula表示:

    Buy Tadalafil 10mg Buy Tadalafil 20mg Generic Tadalafil 20mg price

  5. Charlesbot表示:

    https://maxpillsformen.com/# buy cialis pill

  6. BrettTIT表示:

    Buy generic 100mg Viagra online: FastPillsForMen – Cheapest Sildenafil online

  7. Diplomi_vfml表示:

    как защитить купленный диплом 2orik-diploms.ru .

  8. Barrybib表示:

    Generic Cialis without a doctor prescription: buy cialis online – cheapest cialis

  9. срочное изготовление загранпаспорта срочное изготовление загранпаспорта .

  10. Charlesbot表示:

    https://fastpillsformen.com/# Viagra online price

  11. Charlesbot表示:

    http://maxpillsformen.com/# Tadalafil Tablet

  12. Barrybib表示:

    erectile dysfunction online prescription: cheap cialis – buy ed pills online

  13. Donaldabula表示:

    Cialis without a doctor prescription Generic Cialis without a doctor prescription Buy Tadalafil 10mg

  14. BrettTIT表示:

    Cheap generic Viagra online: FastPillsForMen.com – Viagra tablet online

  15. Donaldabula表示:

    cheapest viagra Fast Pills For Men Cheap Viagra 100mg

  16. Donaldabula表示:

    Generic Viagra online Fast Pills For Men Cheap Viagra 100mg

  17. Barrybib表示:

    Tadalafil Tablet: Generic Cialis without a doctor prescription – Generic Cialis price

  18. Donaldabula表示:

    viagra without prescription buy viagra online cheapest viagra

  19. Charlesbot表示:

    http://fastpillseasy.com/# buying ed pills online

  20. Barrybib表示:

    buy cialis pill: MaxPillsForMen – Buy Cialis online

  21. Therefore, it can store large data that can be a combination of market trends and the industry.

  22. Charlesbot表示:

    http://maxpillsformen.com/# Generic Cialis price

  23. DannyErype表示:

    Ukrainian President Volodymyr Zelensky will meet US President Joe Biden and Vice President Kamala Harris in Washington on Thursday. Leon Neal/Getty Images
    CNN

    Ukrainian President Volodymyr Zelensky’s visit to the White House on Thursday could be his final chance to convince a receptive American president of his country’s war aims.
    [url=https://megaweb-19at.com]megaweb2.at[/url]
    The precise details of the “victory plan” Zelensky plans to present in separate meetings to President Joe Biden and Vice President Kamala Harris are unknown, having been closely held until they are presented to the American leaders.

    But according to people briefed on its broad contours, the plan reflects the Ukrainian leader’s urgent appeals for more immediate help countering Russia’s invasion. Zelensky is also poised to push for long-term security guarantees that could withstand changes in American leadership ahead of what is widely expected to be a close presidential election between Harris and former President Donald Trump.

    The plan, people familiar with it said, acts as Zelensky’s response to growing war weariness even among his staunchest of western allies. It will make the case that Ukraine can still win — and does not need to cede Russian-seized territory for the fighting to end — if enough assistance is rushed in.

    That includes again asking permission to fire Western provided long-range weapons deeper into Russian territory, a line Biden once was loathe to cross but which he’s recently appeared more open to as he has come under growing pressure to relent.

    Even if Biden decides to allow the long-range fires, it’s unclear whether the change in policy would be announced publicly.

    Biden is usually apt to take his time making decisions about providing Ukraine new capabilities. But with November’s election potentially portending a major change in American approach to the war if Trump were to win, Ukrainian officials — and many American ones — believe there is little time to waste.
    mega333mq5acolj7rw726jjy6g3ihgsmnhlfuuk6cd2267jbohhc4aqd.onion
    https://megaweb-20at.com
    Trump has claimed he will be able to “settle” the war upon taking office and has suggested he’ll end US support for Kyiv’s war effort.

    “Those cities are gone, they’re gone, and we continue to give billions of dollars to a man who refused to make a deal, Zelensky. There was no deal that he could have made that wouldn’t have been better than the situation you have right now. You have a country that has been obliterated, not possible to be rebuilt,” Trump said during a campaign speech in Mint Hill, North Carolina, on Wednesday.

    Comments like those have lent new weight to Thursday’s Oval Office talks, according to American and European officials, who have described an imperative to surge assistance to Ukraine while Biden is still in office.

    As part of Zelensky’s visit, the US is expected to announce a major new security package, thought it will likely delay the shipping of the equipment due to inventory shortages, CNN previously reported according to two US officials. On Wednesday, the US announced a package of $375 million.

    The president previewed Zelensky’s visit to the White House a day beforehand, declaring on the margins of the United Nations General Assembly his administration was “determined to ensure that Ukraine has what it needs to prevail in fight for survival.”
    [url=https://megaweb-9.com]megaweb19.com[/url]
    “Tomorrow, I will announce a series of actions to accelerate support for Ukraine’s military – but we know Ukraine’s future victory is about more than what happens on the battlefield, it’s also about what Ukrainians do make the most of a free and independent future, which so many have sacrificed so much for,” he said.

  24. The original Model T, released in 1908, packed a 2.9-liter four-cylinder engine with just 22 horsepower.

  25. BrettTIT表示:

    sildenafil 50 mg price: FastPillsForMen – Viagra tablet online

  26. неотложная наркологическая помощь в москве неотложная наркологическая помощь в москве .

發佈留言

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