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.
我试图从另一台服务器读取xml到一个网页,我认为我的问题是同源策略,因此是一个跨域问题。
I have a bit of googling and it seems that jsonp is the way forward. Based on some examples I found here on * 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.
我有一点谷歌搜索,似乎jsonp是前进的方式。根据我在*和其他网站上找到的一些例子,这就是我所拥有的,并且它不会用xml“命中”服务器。我可以在浏览器中查看xml。
$(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)
有什么建议么?请记住,我是关于JS / JQuery的新手; o)
2 个解决方案
#1
3
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):
如果您无权访问服务器(例如,如果您正在使用api),则可以使用YQL将XML转换为jsonp,并使用自定义自定义YQL URL从浏览器中查询yahoo服务器(其中)嵌入了一个类似SQL的语句)。这是一个例子(对于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'
});
});
#2
1
If you have access to code generating the XML on the remote server, you can wrap the whole thing in jsonp.
如果您可以访问在远程服务器上生成XML的代码,则可以将整个内容包装在jsonp中。
JSONP is a way of getting around the same-origin policy by obtaining data via using <script>
tags rather than trying to remotely extract information.
JSONP是一种通过使用
in your getconfiguation
script, you would have something like
在你的getconfiguation脚本中,你会有类似的东西
callback("SERVER GENERATED XML/JSON DATA GOES HERE");
where the callback is specified by the remote call
其中回调是由远程调用指定的
For instance, if your remote script was php, you would make it look something like this:
例如,如果你的远程脚本是php,你会看起来像这样:
<?php
// getconfiguration.php
echo "$_GET['callback']($configuration_data);"
?>
Then make run AJAX you provided in your question. What this actually does is dynamically insert a script tag into your page like this:
然后运行你在问题中提供的AJAX。这实际上做的是动态地将脚本标记插入到您的页面中,如下所示:
<script src="http://192.168.0.106:8111/getconfiguation.php?callback=???"></script>
jquery fills in the ??? for you with some unique wrapper it generated for your success callback
jquery填写???为您提供一些独特的包装器,它为您的成功回调生成
#1
3
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):
如果您无权访问服务器(例如,如果您正在使用api),则可以使用YQL将XML转换为jsonp,并使用自定义自定义YQL URL从浏览器中查询yahoo服务器(其中)嵌入了一个类似SQL的语句)。这是一个例子(对于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'
});
});
#2
1
If you have access to code generating the XML on the remote server, you can wrap the whole thing in jsonp.
如果您可以访问在远程服务器上生成XML的代码,则可以将整个内容包装在jsonp中。
JSONP is a way of getting around the same-origin policy by obtaining data via using <script>
tags rather than trying to remotely extract information.
JSONP是一种通过使用
in your getconfiguation
script, you would have something like
在你的getconfiguation脚本中,你会有类似的东西
callback("SERVER GENERATED XML/JSON DATA GOES HERE");
where the callback is specified by the remote call
其中回调是由远程调用指定的
For instance, if your remote script was php, you would make it look something like this:
例如,如果你的远程脚本是php,你会看起来像这样:
<?php
// getconfiguration.php
echo "$_GET['callback']($configuration_data);"
?>
Then make run AJAX you provided in your question. What this actually does is dynamically insert a script tag into your page like this:
然后运行你在问题中提供的AJAX。这实际上做的是动态地将脚本标记插入到您的页面中,如下所示:
<script src="http://192.168.0.106:8111/getconfiguation.php?callback=???"></script>
jquery fills in the ??? for you with some unique wrapper it generated for your success callback
jquery填写???为您提供一些独特的包装器,它为您的成功回调生成