Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
AJAX (without a server-side script)
Old 01-25-2009, 01:19 PM AJAX (without a server-side script)
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
I'm trying to modify a function to make an Ajax request directly from an XML file, rather than have a server side script in the middle.

However, the function 'ajaxRespose' doesn't seem to be getting called, I think the problem is the line with:

Code:
ajaxReq.open ('GET', filename);
I'm guessing the 'GET' shouldn't be there, although I've tried omitting it but have had no luck.

Here is the function I am using:
Code:
function ajaxRequest (filename)
    {
        var ajaxReq = false;
        
        try 
            {
                var ajaxReq = new XMLHttpRequest (); }
        
        catch (err)
            {
                ajaxReq = false; }
        
        ajaxReq.open ('GET', filename);
        ajaxReq.onreadystatechange = ajaxResponse;
        ajaxReq.send (null);
    }
Note: the value of the 'filename' variable will be 'data.xml', and I have ensured that the file does exist in the directory.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 01-25-2009, 01:59 PM Re: AJAX (without a server-side script)
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
var ajaxReq = new XMLHttpRequest (); will only work in FF & Opera

ajaxReq.onreadystatechange should point to a function that does something the returned data.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-25-2009, 02:01 PM Re: AJAX (without a server-side script)
vangogh's Avatar
Post Impressionist

Posts: 10,688
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
Could post the code for ajaxResponse? When you say it's not being called do you know that it's not being called or it's just not doing anything? Could be it's not grabbing the info from the XML file properly.
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 01-25-2009, 02:06 PM Re: AJAX (without a server-side script)
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Thanks, yeah here it is:
Code:
function ajaxResponse ()
    {
        if (ajaxReq.readyState != 4)
            {
                return; }
        
        if (ajaxReq.status == 200)
            {
                if (ajaxCallBack)
                    {
                        ajaxCallBack ();
                    }
            }
        
        else
            {
                alert ('Request Failed: ' + ajaxReq.statusText);
            }        
    }

// gather student details

// assign xml filename
function fileAssign ()
    {
        var filename = 'data.xml';
        ajaxCallBack = tableBuild;

        // request data
        ajaxRequest (filename);
    }

function tableBuild ()
    {
        var stu = ajaxReq.responseXML.getElementsByTagName ('name');
        alert (stu[0].firstChild.nodeValue);
    }

fileAssign ();
I've included the following functions aswell, just in case
Also, I think the ajaxResponse function isn't being called because I added a simple alert at the start of the function, which wasn't displayed.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Last edited by pealo86; 01-25-2009 at 02:07 PM..
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 01-25-2009, 02:12 PM Re: AJAX (without a server-side script)
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
var ajaxReq = false; in a function, would declare a local variable, only available to that function.
So you either have to remove the var keyword, or pass the variable along with calling the next function.

And ajaxReq.open(); takes three arguments:
- method
- file
- asynchronous (true/false)
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>

Last edited by Insensus; 01-25-2009 at 02:14 PM..
Insensus is offline
Reply With Quote
View Public Profile
 
Old 01-27-2009, 11:36 AM Re: AJAX (without a server-side script)
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Quote:
Originally Posted by Insensus View Post
var ajaxReq = false; in a function, would declare a local variable, only available to that function.
So you either have to remove the var keyword, or pass the variable along with calling the next function.

And ajaxReq.open(); takes three arguments:
- method
- file
- asynchronous (true/false)

Hmmm that's strange, because I've used the script before with the var keyword and it's worked. The only difference with this one really is that I'm not reading from a PHP file.

I've tried ajaxReq.open with the 'POST' method but that didn't work either (although I wasn't expecting it to anyway!)
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 01-27-2009, 12:57 PM Re: AJAX (without a server-side script)
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
The point you seem to be missing is that in your code ajaxReq isn't going to exist in ajaxResponse() because it is a local variable to ajaxRequest()

move the instantiation of the variable outside of the function and it will work (but only in firefox)

http://www.modtalk.co.uk/_site/code/javascript/getxml/
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-29-2009, 12:46 PM Re: AJAX (without a server-side script)
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
At last it works! Thanks. Turns out the code in the book I have was the way you said to do it... only I read it wrong!
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Reply     « Reply to AJAX (without a server-side script)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.05276 seconds with 12 queries