Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

xss - use jsonp to get xml cross domain

I am trying to read xml into a webpage from another server, and I assume that my problem is Same-Origin Policy, and therefore a cross domain problem.

I have a bit of googling and it seems that jsonp is the way forward. Based on some examples I found here on stackoverflow and another sites, this is what I have, and it does not "hit" the server with the xml. I can view the xml in a browser.

$(document).ready(function(){    
   $.ajax({
        type: 'GET',
        dataType: 'jsonp',                
        url: 'http://192.168.0.106:8111/getconfiguration?',
        success: function (xml) 
        { //do stuff with received xml 
        }});    

Any suggestions? please keep in mind that I am a newbie with regards to JS / JQuery ;o)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you don't have access to the server (if, for example, you're consuming an api) you can use YQL to convert your XML to jsonp and query the yahoo server from the browser using a custom custom YQL url (in which is embedded a SQL-like statement). Here's an example (for the zillow api):

$('document').ready(function(){
  $.ajax({
    url: 'http://query.yahooapis.com/v1/public/yql?q=select * from zillow.search where address = "1835 73rd Ave NE" and citystatezip = "98039" and zwsid = "X1-ZWz1cse68iatcb_13bwv"&format=json&diagnostics=true&env=http://datatables.org/alltables.env&callback=mydata',
    jsonpCallback: "mydata",
    success: function(results) {
      console.log(results.query.results.searchresults.response.results.result.zpid);
    },
    dataType: 'jsonp'
  });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...