Ajax cross domain question

眉间皱痕 提交于 2020-01-06 08:47:09

问题


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>


回答1:


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.



来源:https://stackoverflow.com/questions/1974452/ajax-cross-domain-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!