YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Connection Manager: Connection Manager GET Transaction

# Connection Manager: Connection Manager GET Transaction

To create a GET transaction using Connection Manager, you will need to construct a querystring of key-value pairs and append it to the URL. The following code example provides a step-by-step approach to creating a simple GET transaction.

Click "Send a GET Request" below to try it out, then read the description below to learn how to create a simple transaction using Connection Manager.

View example in new window.

## Using Connection Manager to Retrieve a File via HTTP GET

### Source file and dependencies

Load the Yahoo Global Object and Connection Manager source files:

<script src="yahoo.js"></script> <script src="event.js"></script> <script src="connection.js"></script>

### Assemble the Querystring

Construct a querystring with two key-value pairs of `username = anonymous` and `userid = 0`:

/* * * Create a querystring with example key-value pairs of * username and userid. Remember to encode the querystring * if and when the string contains special characters. * */ var sUrl = "php/get.php?username=anonymous&userid=0";

### The Callback Object

Create a callback object to handle the response, and pass an object literal to both `success` and `failure` handlers as the argument.

var div = document.getElementById('container'); var handleSuccess = function(o){ if(o.responseText !== undefined){ div.innerHTML = "<li>Transaction id: " + o.tId + "</li>"; div.innerHTML += "<li>HTTP status: " + o.status + "</li>"; div.innerHTML += "<li>Status code message: " + o.statusText + "</li>"; div.innerHTML += "<li>HTTP headers: <ul>" + o.getAllResponseHeaders + "</ul></li>"; div.innerHTML += "<li>Server response: " + o.responseText + "</li>"; div.innerHTML += "<li>Argument object: Object ( [foo] => " + o.argument.foo + " [bar] => " + o.argument.bar +" )</li>"; } } var handleFailure = function(o){ if(o.responseText !== undefined){ div.innerHTML = "<li>Transaction id: " + o.tId + "</li>"; div.innerHTML += "<li>HTTP status: " + o.status + "</li>"; div.innerHTML += "<li>Status code message: " + o.statusText + "</li>"; } } var callback = { success:handleSuccess, failure: handleFailure, argument: { foo:"foo", bar:"bar" } };

### Initiate the GET Transaction

Call `YAHOO.util.Connect.asyncRequest` to send the request to `get.php`, and the PHP file will return the output of `$_POST` via `print_r()`. The `handleSuccess` callback will print the response object's properties, including the server response data.

var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### Connection Manager Examples:

### More Connection Manager Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings