1

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>

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.