Simple AJAX help

Status
Not open for further replies.

Teensweb

New Member
Messages
352
Reaction score
1
Points
0
I have an ajax code which fetches a page to a division. It is as follows:
fetcher.js-
Code:
function fetch(URL, divId) {
req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");

req.open("GET", URL, true);
req.onreadystatechange = function() {
if (req.readyState == 4 && req.status == 200) {
document.getElementById(divId).innerHTML = req.responseText;
}
}
req.send(null);
}
Html-
HTML:
<script language="javascript" src="fetcher.js"></script>
<div id="container_div" class="container">Some default text<div>
<a href="#" onclick="fetch('somepage.php')">fetch</a>
It works fine, but the problem is that it won't fetch any remote url's that is, if I give http://google.com or something, it won't work.
Can u give a solution?
 

VPmase

New Member
Messages
914
Reaction score
1
Points
0
The reason why it won't fetch URLS is because most of them don't echo or print anything so there really isn't a responseText variable.
 

wizkid

New Member
Messages
25
Reaction score
0
Points
0
you should check syntax of the function "fetch"
you define it with two arguements(URL, divId) but only pass one variable to it(URL) hence divId has a null value and the response text variable will not exist
 

Chris Z

Active Member
Messages
5,603
Reaction score
0
Points
36
This is totally unrelated to AJAX but maybe you could use a PHP Function like cURL to get the site you request.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
As far as I know, you can't make requests to remote servers through AJAX, at least not in any popular browser. It's considered a security weakness to allow that. Apparently they're alright with JS being able to redirect users, load iframes to any url, and not to mention create infinite loops and all the other annoying stuff. But AJAX connections to remote servers is where they draw the incredibly distorted line.

The best workaround for this is to make a server-side script get the content. Just be sure to impose the right restrictions.
 

TechAsh

Retired
Messages
5,853
Reaction score
7
Points
38
The best workaround for this is to make a server-side script get the content.
I don't recommend doing that as it would be a proxy, which isn't allowed on x10Hosting.

EDIT:
This is my 1400th post.
 
Last edited:

tnl2k7

Banned
Messages
3,131
Reaction score
0
Points
0
Why do you need to get data from a remote website? Isn't there any other way to accomplish this? It just seems a little odd that you're collecting data from another source. Can't you just use an <iframe>? They might be awkward but sometimes they are the best way to do things.

This is my 1,300th post, by the way.

-Luke.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
I don't recommend doing that as it would be a proxy, which isn't allowed on x10Hosting.

EDIT:
This is my 1400th post.

There's a difference between a proxy and merely getting content from remote servers. The OP may very well just need to get data from a few sites(similar to that cpanel script you were using before). Yes, it could be possible for a user to use the script to get data from any server if it wasn't written well. That's why I said the proper restrictions should be imposed. But that's true even if proxies weren't allowed by x10.

This is my 303rd post :p
 

Teensweb

New Member
Messages
352
Reaction score
1
Points
0
I just wanted to start a rating star service so that users could put a js code that would retrieve data from my page. But no need now. I managed it with an iframe
 

tnl2k7

Banned
Messages
3,131
Reaction score
0
Points
0
Seeing as this has been resolved now then, I'll close it. Feel free to make a new thread if you need help :).

-Luke.

* Thread Closed *
 
Status
Not open for further replies.
Top