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
Basic Ajax XMLHttpRequest
Old 08-19-2010, 12:43 AM Basic Ajax XMLHttpRequest
ericson3000's Avatar
Experienced Talker

Posts: 40
Name: Bob Davis
Location: Los Angeles, CA
Trades: 0
Can someone help me out and take a look at my code snippet to see if it looks like a correct Object Request. I really appreciate any input. I want to have something CORRECT on hand to use when I need a starting point.

Code:
// basic variable setup
window.onload = createRequest; // runs the function when the page loads
var xhr = false; // makes sure xhr is false to begin
var url = "whatever.xml"; // this will be the link called in the open method later


// CREATE THE REQUEST
function createRequest () {
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
        }
        else {
            if (window.ActiveXObject) {
                try {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) { }
            }
        }
        // provides the information needed to properly create the request
        if (xhr) {
            xhr.onreadystatechange = showContents; // references the function showContents (BELOW)
            xhr.open("GET", url, true); // true is asynchronous
            xhr.send(null);
        }
        else {
            window.alert("Sorry, but I couldn't create an XMLHttpRequest"); // the request could not be created
    }
}
    
// DISPLAY THE REQUESTED INFORMATION - once the request has been created    
function showContents () {    
    if (xhr.readyState == 4) { // the request has finished loading
        if (xhr.status == 200) { // the information is okay
            var outMsg = xhr.responseText; // assigns a variable; this could be responseText OR responseXML
            // if necessary, other code could be placed here that will eventually be used
        }
        else {
            var outMsg = "There was a problem with the request " + xhr.status; // this is the error message
        }
    
            // Put code here to do something with outMsg
    }
}

Last edited by chrishirst; 08-19-2010 at 03:25 PM..
ericson3000 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-24-2010, 07:47 AM Re: Basic Ajax XMLHttpRequest
Extreme Talker

Posts: 246
Trades: 0
Have you tested it, and if so, what doesn't work. What error are you getting?
__________________

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
stbuchok is offline
Reply With Quote
View Public Profile
 
Old 08-24-2010, 08:54 AM Re: Basic Ajax XMLHttpRequest
lynxus's Avatar
Awesomeo-Maximo

Posts: 1,618
Location: UK
Trades: 1
Checkout my blog on this,
It may work better for you:

http://www.hauntednipple.co.uk/?p=16
__________________

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


lynxus is offline
Reply With Quote
View Public Profile Visit lynxus's homepage!
 
Old 08-31-2010, 12:39 AM Re: Basic Ajax XMLHttpRequest
Novice Talker

Posts: 5
Name: jackrog
Trades: 0
The basic of Ajax XML Http request for these there are so many tutorials can be available on the internet with having so many advance facility it into them. These all are really great to share with us.
__________________

Please login or register to view this content. Registration is FREE
jackrog is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Basic Ajax XMLHttpRequest
 

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 0.23510 seconds with 12 queries