使用另一个域上另一个页面的字段值填充字段
我正在尝试创建一个字段,该字段将从位于另一个域的另一个页面上的另一个字段中提取值。
有 2 个字段:#div-one(在第一页)和 #div-two(在第二页)
有两个页面:第一页(在域“One.com”上)和第二页(在域上) “Two.com”)
当用户打开第一页时,我希望 #div-one 提取 Two.com 上第二页上的 #div-two 内的值。
我找到了这段代码:
<script type="text/javascript">
function pullValueFromPageTwo(){
var val = $('[#div-two]').text();
$('#div-one').val(val);
}
</script>
但我没有找到任何关于如何使用该代码使用 jquery 从另一个页面和域中提取值的线索。
I'm trying to create a field which would pull values from another field on another page that's located on another domain.
There are 2 fields: #div-one (on page-one) and #div-two (on page-two)
There are two pages: page-one (on domain "One.com") and page-two (on domain "Two.com")
When a user opens page-one I want #div-one to pull the value that is inside #div-two on page-two located on Two.com.
I found this code:
<script type="text/javascript">
function pullValueFromPageTwo(){
var val = $('[#div-two]').text();
$('#div-one').val(val);
}
</script>
But I didn't found any clue of how to use that code to pull a value from another page and domain using jquery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JSONP 是另一种规避同源策略的方法。 jQuery 1.5 改进了对其的支持。
http://api.jquery.com/jQuery.getJSON/
http://en.wikipedia.org/wiki/JSON#JSONP
JSONP is another method for circumventing same origin policies. jQuery 1.5 has improved support for it.
http://api.jquery.com/jQuery.getJSON/
http://en.wikipedia.org/wiki/JSON#JSONP
或者您可以使用其他东西从 Two.com 获取数据(例如 YQL {http://developer.yahoo.com/yql/}),然后使用 javascript 处理结果(因为它返回 XML,这是很有可能的) ..
一个示例查询,您可以将其放入地址栏中以查看结果:
然后,您可以使用一点 ajax 从 One.com 上的 Two.com 获取值,并用它执行您想要的操作。
Or you could get the data from Two.com with something else (YQL {http://developer.yahoo.com/yql/} for example) and then work with the results using javascript (as it returns XML this is very possible)..
An example query that you can put in the address bar to see the results of:
You can then get the value from Two.com on One.com with a little bit of ajax and do what you want with it.
One.com
上的 jQuery 无法访问Two.com
,因为 同源政策。但是,如果它们位于同一个域中,这样的事情应该可以工作......
jQuery on
One.com
can not accessTwo.com
because of same origin policy.However, if they were on the same domain, something like this should work...