All pastes #2070605 Raw Edit

Miscellany

public text v1 · immutable
#2070605 ·published 2011-05-27 21:16 UTC
rendered paste body
function getSiteAddresses()
{
    var req = newXMLHttpRequest();

    // Set the handler function to receive callback notifications
    // from the request object
    var handlerFunction = getReadyStateHandler(req);
    req.onreadystatechange = handlerFunction;

    // Open an HTTP POST connection to the shopping cart servlet.
    // Third parameter specifies request is asynchronous.
    req.open("POST", "test.do", true);

    // Specify that the body of the request contains form data
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    // Send form encoded data stating that I want to add the
    // specified item to the cart.
    req.send("method=getSiteAddresses");
}

/*
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes its JSON response
 * to the given handler function.
 * req - The XMLHttpRequest whose state is changing
 * responseHTMLHandler - Function to pass the HTML response to
 */
function getReadyStateHandler(req)
{
    // Return an anonymous function that listens to the
    // XMLHttpRequest instance
    return function ()
    {
        // If the request's status is "complete"
        if (req.readyState == 4)
        {
            // Check that a successful server response was received
            if (req.status == 200)
            {
                // Pass the JSON payload of the response to the
                // handler function
                populateDatagrid(req.responseText.toJSON());
            }
            else
            {
                // An HTTP problem has occurred
                alert("HTTP error: " + req.status);
            }
        }
    }
}

function populateDatagrid(addressesJSON)
{
    var addresses = jQuery.parseJSON(addressesJSON);
    var tbody     = document.getElementById("dataGridBody");
    //msgBox("Updating datagrid... with info: Test 1 " + addresses.address); // Undefined
    //msgBox("Updating datagrid... with info: Test 2 " + addresses[0]); // Undefined
    //msgBox("Updating datagrid... with info: Test 3 " + addresses[0].address); // Undefined
    //msgBox("Updating datagrid... with info: Test 4 " + addressesJSON); // ?
    for (i=0 ; i<addresses.Addresses.length ; i++)
    {
        // Create the row that this site will be displayed on
        var row = document.createElement("tr");
        var address = JSON.parse(addresses.Addresses);
        // Create each Table Data (Column)
        for (e=0 ; e<address.length ; e++ )
        {
            var column = document.createElement("td");
            column.innerText = address[e];
            row.appendChild(column);
        }
        tbody.appendChild(row);
    }
}