Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

AJAX request to another site - FAILED

45 views
Skip to first unread message

TuanSandman

unread,
Jul 17, 2009, 2:03:20 PM7/17/09
to ngoan...@gmail.com
hi there

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>


Sasha Malahov

unread,
Jul 17, 2009, 3:49:17 PM7/17/09
to TuanSandman, dev-tech-...@lists.mozilla.org
You are not allowed to make AJAX request to pages that do not belong to
you.

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

Evandro Myller

unread,
Jul 18, 2009, 5:33:08 PM7/18/09
to TuanSandman, dev-tech-...@lists.mozilla.org
Just two tips:

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/

0 new messages