/    Sign up×
Community /Pin to ProfileBookmark

AJAX for noobie

Hey,

I bit the bullet and needed to use ajax to check if a page exists, as I am writing a link manager for someone.

[code=php]

var fail = false;
var http_request = false;
var url = document.getElementsByName(‘txtURL’)[0].value;

function check_link(){

//AJAX it!
if (window.XMLHttpRequest){
http_request = new XMLHttpRequest(); //set the request
http_request.onreadystatechange = is_valid_url; //function to call on each set
http_request.open(“GET”, url, true); //set the page to request
http_request.send(null); //initialize the request
}
//ActiveX – can we say IE?
else if(window.ActiveXObject){
//Create our RequestObject
http_request = new ActiveXObject(“Microsoft.XMLHTTP”);
if(http_request){
http_request.onreadystatechange = is_valid_url //function to call on each step
http_request.open(“GET”, url, false); //set the page to request
http_request.send(null); //initialize the request
}
}

if(fail){
alert(‘failed’);
return false;
}else{
alert(‘passed’);
return true;
}
}

function is_valid_url(){
if (http_request.readyState == 4){
if(http_request.status != 200)
fail = true;
else
fail = false;

return;
}
}

[/code]

ok, that is the basis of my code.

If I check readyState, I get on error of ‘Access Denied’ on the line that sets the function to the onreadystatechange.

If I remove the check for readyState, I get an ‘Unspecified Error’ in the is_valid_url function when it tries to check http_request.status.

Any help appreciated,

CM!

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceMay 12.2006 — I believe you have to set the [b]onreadystatechange[/b] event *after* the open().
Copy linkTweet thisAlerts:
@CrazyMerlinauthorMay 14.2006 — I don't believe it matter where you set the open and onreadystate, as these are only configuration.

The send(null) is the actual initialization.

Don't worry, I'll find a tutorial see if I'm doing something wrong.

Thx
Copy linkTweet thisAlerts:
@UltimaterMay 14.2006 — "false" to "true".
<i>
</i> if(http_request){
http_request.onreadystatechange = is_valid_url //function to call on each step
http_request.open("GET", url, [color=blue]true[/color]); //set the page to request
http_request.send(null); //initialize the request
}

Also your "txtURL" field most contain a URL within the same domain as the script running.
Copy linkTweet thisAlerts:
@CrazyMerlinauthorMay 14.2006 — really?

so you cannot use AJAX to get data from a different site?

there is a table of live data published by another company that I need to access on the fly.

I can do it using PHP, but I thought maybe AJAX could do it.

oh well, back to php.
Copy linkTweet thisAlerts:
@phpnoviceMay 14.2006 — so you cannot use AJAX to get data from a different site?[/QUOTE]
This is a browser security setting that needs changing in order to use XMLHttpRequest across domains. Of course, you cannot change this via JavaScript; so, such a solution would not help visitors to a website that attempts to employ Ajax cross-domain.
Copy linkTweet thisAlerts:
@felgallMay 14.2006 — You can call a server side script on your own site that then performs the test against the other site and returns the result.
Copy linkTweet thisAlerts:
@CrazyMerlinauthorMay 15.2006 — I think I would just be better sticking to php, getting the file into a variable and stripping out everything I don't need.

The page is just 3 tables...I need the one in the middle, and the developers at the other end decided not to give the tables ID attributes.

Luckily the format is the same all the time as the page is script-written.

Thx for your help guys!

CM
×

Success!

Help @CrazyMerlin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...