Ajax跨域问题 [英] Ajax cross domain question
问题描述
在程式码中有两种方法,第一种方法应从同一个网域(即example.com)读取文字,第二个功能应从不同的网域(Google.com/example.txt)读取文字。可以任何让我知道谁做这个。我不知道我是否正确地提出了问题。请问我是否不明白我的问题。
In the code there are two methods, 1st method should read the text from the same domain that is example.com, and the 2nd function should read the text from different domain that Google.com/example.txt. Could any please let me know who to do this. I'm not sure whether I have framed the question properly. Please ask me if you do not understand my question.
//Ajax Question
//The html file path is http://example.com/example.html
<html>
<head>
<script type="text/javascript">
function Click1()
{
var div=// read the text from http://example.com/example.txt
document.getElementById("div1").innerHTML = div;
}
function Click2()
{
var div=// read the text from http://google.com/example.txt
document.getElementById("div2").innerHTML = div;
}
</script>
<body>
<input type="Button" Value="Button 1"name="textbox" onClick="Click1();"/>
<div id="div1">
</div>
<input type="Button" Value="Button 2"name="textbox" onClick="Click2();"/>
<div id="div2">
</div>
推荐答案
这不能使用纯脚本技术来完成。实现它的一种方法是在 example.com
上编写一个服务器端脚本,作为到其他域的桥梁,并执行ajax调用 example.com/bridge.cgi
。如果您可以控制其他网域,您也可以使用 JSONP 依赖于 XHR ,而是包含脚本
标记插入DOM,因此仅限于GET请求。
This cannot be accomplished using pure scripting technology. One way to achieve it is to write a server side script on example.com
that will serve as a bridge to the other domain and perform the ajax call to example.com/bridge.cgi
. In case you have control over the other domain you could also use JSONP which doesn't rely on XHR but instead it includes a script
tag into the DOM and thus is limited to GET requests only.
这篇关于Ajax跨域问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!