Listing Creating a Simple Access Page

function GetData() {

// Create the request.

var Req = new ActiveX0bject("Msxml2.ServerXMLHTTP.4.0");"P0ST", "", false);

// Define the header information for the request. These headers // appear in the same order recommended by the documentation at // Req.setRequestHeader("X-EBAY-API-SESSION-CERTIFICATE",

SubmissionForm.DevIDStr.value + ";" + SubmissionForm.AppIDStr.value + ";" + SubmissionForm.CertIDStr.value); Req.setRequestHeader("X-EBAY-API-COMPATIBILITY-LEVEL", "305"); Req.setRequestHeader("X-EBAY-API-DEV-NAME",

SubmissionForm.DevIDStr.value); Req.setRequestHeader("X-EBAY-API-APP-NAME",

Submi ssionForm.AppIDStr.value); Req.setRequestHeader("X-EBAY-API-CERT-NAME",

SubmissionForm.CertIDStr.value); Req.setRequestHeader("X-EBAY-API-CALL-NAME", "GetSearchResults"); Req.setRequestHeader("X-EBAY-API-SITEID", "0"); Req.setRequestHeader("X-EBAY-API-DETAIL-LEVEL", "0"); Req.setRequestHeader("Content-Type", "text/xml");

// Create the XML request object.

var XMLData = new ActiveXObject("Msxm12.DOMDocument.4.0");

// Add the XML request header, var ProcInst =


"xml", "version=\"1.0\" encoding=\"iso-8859-1\""); XMLData.appendChild(ProcInst);

// Construct the XML request message.

var Root = XMLData.createE1ement("request");

Root.setAttribute("xm1ns", "urn:eBayAPIschema");

// Add the user information.

var Elem = XMLData.createE1ement("RequestUserId"); Elem.text = SubmissionForm.UserIDStr.value; Root.appendChild(Elem);

Elem = XMLData.createE1ement("RequestPassword"); Elem.text = SubmissionForm.PasswordStr.value; Root.appendChild(Elem);

// Add the request data.

Elem = XMLData.createE1ement("Detai1Leve1");


Elem = XMLData.createE1ement("ErrorLeve1");


Elem = XMLData.createE1ement("Query");

Elem.text = SubmissionForm.QueryStr.value;


Elem = XMLData.createE1ement("SiteId");


Elem = XMLData.createE1ement("Verb");

Elem.text = "GetSearchResults";


// Add the request data to the XML request. XMLData.appendChild(Root);

// Send the request. Req.send(XMLData);

// Write the data to screen, if (Req.status == 200)

SubmissionForm.ResultStr.value = Req.responseText; else a1ert("Request Failed!");

The code begins by creating an XML HTTP request object, Req. It uses Req to open a connection with the sandbox server that relies on the POST technique with the open() method. The only code you'd change when working with the production server is the URL for the open() method.

The next task is to build a series of headers using the setRequestHeader() method. There's no law that says you must submit the headers in the order shown. However, submitting them in this order ensures you don't miss a header. eBay Web Services requires that you provide all of the headers shown in the example. The X-EBAY-API-SESSION-CERTIFICATE header is actually a combination of the three keys that eBay provides. You'll also need to provide these keys as separate headers. You must provide the correct X-EBAY-API-CALL-NAME header, even though this value is provided as part of the XML message; otherwise, eBay sends a somewhat ambiguous message telling you there's something wrong with the header, but not the precise problem. You'll also want to adjust the headers for the compatibility level, detail level, and site identifier to meet your specific needs. The only header that will never change is Content-Type.

You can use a number of techniques to create the XML message. The example shows the least error-prone approach based on a lot of experimentation. The code begins by creating a document to hold all of the data. This document doesn't include the XML header (a processing instruction), so you need to add it. Notice that the processing instruction includes both the version number and encoding. The code adds the processing instruction to the XML document.

The next task is to create the root node, <request>. The code sets the namespace attribute so the XML parser knows how to interpret the remaining entries in the document. Notice the code doesn't add the root node to the document, at this point, because the root node contains child nodes that describe the actual request.

A request begins with the user information. This example shows the user identifier and password technique. Other examples in the chapter will show the authentication token technique. No matter which technique you use, you must provide the user information to make a request.

There are exceptions to every rule. You can make three eBay Web Services API calls without user identification. These calls include GetRuName(), SetReturnUrl(), and GetReturnUrl(). The reason you don't need user information for these calls is that the user isn't involved— these are application-specific calls.

The code builds the request next. Again, the actual order of the entries isn't essential, but placing them in order ensures you don't forget anything. The eBay API Test Tool (described in the "Using the eBay API Test Tool" section of Chapter 4) provides one of the better ways to discover what eBay Web Services requires as input. The example begins with all of the essential elements including the detail level, error level, site identifier, and verb (the action you want eBay to perform). Because this is a search request, the code also adds the <Query> element to the root node.

Now that the root node is complete, the code finally adds it to the XML document. The Req.sendQ method transmits the data to the server. Many programming languages would send the request and automatically generate an error should one occur. JavaScript doesn't appear to provide this functionality, so you must check the error status of the communication before you try to do anything with the response. When Req.status equals 200, the communication is successful, even if the actual request failed.

Don't confuse the communication status with the request status. The communication status information only refers to the ability of your application to send data to the server and the server's ability to receive the document. The request status tells about all other forms of error, including most header errors. Consequently, the code must make two checks to ensure it receives a valid response from the server.

At this point, you'd normally check the error status of the response and then process the data in some way or ask the user to try the request again. The example displays the raw XML contained in the Req.responseText property on screen.

eBay Cash Insider

eBay Cash Insider

I refined my information, added notes as things came up and took out old or outdated information. Each time I discovered a new little tip or trick as I was making money with EBay, I added that information. Today, I bring home anywhere from 500 to 2000 each week with EBay. Thats not too shabby for someone who was just a year ago afraid to even purchase something from the site.

Get My Free Ebook

Post a comment