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

  1. Thanks for the tips you discuss through this site. In addition, many young women which become pregnant never even seek to get health care insurance because they are full of fearfulness they probably would not qualify. Although a few states currently require that insurers give coverage despite the pre-existing conditions. Costs on most of these guaranteed programs are usually greater, but when taking into consideration the high cost of medical care bills it may be a new safer strategy to use to protect your current financial potential.

  2. Stanleyinolf表示:

    lisinopril 80 mg cheap lisinopril prinivil

  3. Stanleyinolf表示:

    lisinopril mexico cheap lisinopril lisinopril cost us

  4. Jamesric表示:

    lipitor 200 mg: Atorvastatin 20 mg buy online – lipitor 10mg price comparison

  5. I blog often and I truly appreciate your content. The article has truly peaked my interest. I will bookmark your site and keep checking for new information about once per week. I opted in for your Feed as well.

  6. Explore fashionable styles at mammut clothing.

  7. 먹튀검증表示:

    Aw, this was an incredibly good post. Finding the time and actual effort to generate a superb article… but what can I say… I put things off a lot and never manage to get nearly anything done.

  8. Thanks for the suggestions you have provided here. On top of that, I believe there are some factors that keep your auto insurance premium all the way down. One is, to take into account buying autos that are from the good directory of car insurance companies. Cars which can be expensive will be more at risk of being stolen. Aside from that insurance is also in line with the value of the car, so the higher in price it is, then the higher a premium you pay.

  9. soulmate 2024表示:

    I have seen that right now, more and more people are increasingly being attracted to cameras and the subject of photography. However, being a photographer, you will need to first invest so much time frame deciding the model of digicam to buy along with moving out of store to store just so you could buy the least expensive camera of the trademark you have decided to decide on. But it would not end right now there. You also have to contemplate whether you should obtain a digital digicam extended warranty. Thanks alot : ) for the good recommendations I acquired from your website.

  10. Jamesric表示:

    lipitor 10mg tablet price: buy lipitor 10 mg – lipitor prescription

  11. Everyone loves it when people get together and share ideas. Great website, keep it up!

  12. DanielTet表示:

    п»їcytotec pills online http://cytotec.pro/# buy cytotec
    lasix medication

  13. Hi would you mind letting me know which hosting company you’re working with? I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot quicker then most. Can you recommend a good internet hosting provider at a reasonable price? Thanks a lot, I appreciate it!

  14. Spot on with this write-up, I truly feel this website needs much more attention. I’ll probably be returning to read through more, thanks for the information.

  15. Thanks a lot for the helpful content. It is also my opinion that mesothelioma cancer has an particularly long latency period, which means that the signs of the disease may not emerge until finally 30 to 50 years after the original exposure to asbestos. Pleural mesothelioma, which is the most common form and has effects on the area across the lungs, might cause shortness of breath, chest pains, along with a persistent coughing, which may bring about coughing up our blood.

  16. Jamesric表示:

    lipitor 10mg price: Lipitor 10 mg price – lipitor purchase online

  17. I’m amazed, I have to admit. Seldom do I come across a blog that’s equally educative and amusing, and without a doubt, you’ve hit the nail on the head. The issue is something that too few folks are speaking intelligently about. Now i’m very happy that I stumbled across this in my search for something concerning this.

  18. DanielTet表示:

    Cytotec 200mcg price https://furosemide.win/# furosemida 40 mg
    lasix

  19. JustinBreve表示:

    заказать seo продвижение сайта https://process-seo.ru

  20. Thanks for your publication. One other thing is the fact individual American states have their very own laws that affect homeowners, which makes it extremely tough for the our elected representatives to come up with a whole new set of recommendations concerning home foreclosure on people. The problem is that a state has own laws which may have interaction in an unwanted manner on the subject of foreclosure procedures.

  21. That is a great tip particularly to those fresh to the blogosphere. Brief but very accurate info… Appreciate your sharing this one. A must read article.

  22. Stephenzek表示:

    https://cytotec.pro/# buy cytotec over the counter

  23. Thanks for expressing your ideas on this blog. In addition, a fable regarding the banking companies intentions when talking about foreclosed is that the lender will not take my payments. There is a fair bit of time which the bank can take payments every now and then. If you are as well deep within the hole, they will commonly demand that you pay the actual payment in full. However, i am not saying that they will not take any sort of payments at all. In case you and the bank can manage to work one thing out, a foreclosure method may cease. However, in case you continue to miss payments wih the new plan, the foreclosures process can just pick up where it was left off.

  24. DanielTet表示:

    buy cytotec in usa https://lisinopril.guru/# medicine lisinopril 10 mg
    furosemide 40mg

  25. Jamesric表示:

    buy cheap lisinopril 40 mg no prescription: cheap lisinopril – average cost of lisinopril

發佈留言

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