views:

21

answers:

2

Below code is working properly now. But if I replace 'text.txt' with 'http://google.com' isn't showing anything, nor displaying an error.

What am I doing wrong?

I need this code to get content of an url to a string, on client side.

Thakns.

<script type="text/javascript">

var webUrl = 'text.txt';
var queryString = '';
var xmlText = getAjaxValues(webUrl, queryString);
window.alert(xmlText); 
document.write(xmlText);

function getAjaxValues(webUrl, queryString)
{
 var xmlHttpObject = new XMLHttpRequest();


 xmlHttpObject.open("GET", webUrl, false);
 xmlHttpObject.send();

 var xmlText = xmlHttpObject.responseText;

 return xmlText;
}

</script>
A: 

See the same origin policy

David Dorward
A: 

It's being prevented by the same origin policy, which requires that any AJAX requests, except for scripts and, by extension, jsonp, be made to servers in the same domain as the original page request. Your best bet is to create a proxy method on your server that can accept the url that you want to get the content of and have it request the page and pass it back to the client.

tvanfosson
So how can I get contents of an url to a string on client side?The content must not be parsed through server side.
paul