XMLHttpRequest not working with http
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 XM开发者_开发技巧LHttpRequest();
xmlHttpObject.open("GET", webUrl, false);
xmlHttpObject.send();
var xmlText = xmlHttpObject.responseText;
return xmlText;
}
</script>
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.
精彩评论