I write JS to create AJAX request to another site but I got error.
Does anyone know why. Please help me :)
the test page's here http://www.box.net/shared/npdj71qnt7
------------------------
<html>
<head>
<script language="javascript" type="text/javascript">
function getUrlContent(url) {
var page_request = false;
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest();
else if (window.ActiveXObject) { // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
} else
return false;
page_request.open('GET', url, true); //get page synchronously
page_request.send("");
return page_request.responseText;
} // end of getUrlContent(String)
function request() {
var str = getUrlContent("http://www.google.com");
alert( str );
/*use this with prototype javascript library
new Ajax.Request( "https://www.google.com", {
method: 'get',
asynchronous: false,
onComplete: function( transport ) {
alert( transport.status );
var str = '{"result":' + transport.responseText + '}';
alert( str );
}
});*/
}
</script>
<title>Untitled</title>
</head>
<body>
Hello World!
<input type="button" onclick="request();" />
</body>
</html>
That shouldn't discourage you though. You can make an Ajax call to one
of your pages and in turn make requests to that external site from
within your server-side code.
Thanks, Sasha
hi there
------------------------
alert( str );
<body>
_______________________________________________
dev-tech-javascript mailing list
dev-tech-...@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-tech-javascript
1. There is no sense in using an ajax callback when you make synchronous
requests. We can say that a synchronous request means that its following
code will be ran only when it gets finished.
2. Keep all your JavaScript code out of the <head> tag. Why?
http://emyller.net/blog/2008/09/script-tag-on-head-can-be-evil/
Happy coding. ;)
> _______________________________________________
> dev-tech-javascript mailing list
> dev-tech-...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-javascript
>
--
E. Myller
Front-end engineer / architect
http://emyller.net/