JavaScript Engine: XMLHTTPRequest example

Note: This topic applies to Web - HTTP/HTML Vuser scripts written in C only.

VuGen's JavaScript Engine enables you to include JavaScript code in a Vuser script. For details on the JavaScript Engine, see Using the VuGen JavaScript engine.

The example below shows how you can use a JavaScript XMLHTTPRequest object in a Web(HTTP/HTML) Vuser script. In this example, the XMLHTTPRequest object enables the Vuser to download a stock quote from finance.example.com, and then to save the value to a parameter for future use.

The script section below shows a web_js_run function that has been inserted into a Vuser script. The web_js_run function includes a reference to a file called XMLHTTPRequest_sync_sample.js. This file contains the JavaScript code that executes the XMLHTTPRequest function.

Copy code

web.js_run(

web.js_run(
    "Code=getQuotes(LR.getParam('symbol'));",
    "ResultParam=param",
    SOURCES,
    "File=XMLHTTPRequest_sync_sample.js"
    ENDITEM,
    LAST
);

The content of the XMLHTTPRequest_sync_sample.js file is shown below.

Copy code
var req2;

function getQuotes(mySymbol) {
    var myURL = "http://download.finance.example.com/d/quotes.csv?s=" + mySymbol + "&f=1&e=.csv";
    
    req2 = false;
    // Branch for native XMLHttpRequest object
    try {
        req2 = new XMLHttpRequest();
    } catch(e) {
        req2 = false;
    }
    
    if(req2) {
        req2.open("GET", myURL, false);
        req2.send("");
    }
    return 1 * req2.responseText;
}
  • For additional examples of code used with the JavaScript Engine, see the Function Reference.
Back to top