views:

36

answers:

1

Hi Everyone,

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>

+2  A: 

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.

Darin Dimitrov
Thanks Darin, I will try this.
Santosh