Web Browser JavaScript Example | Teradata Query Service - Web Browser JavaScript Example - Teradata Query Service

Teradata® Query Service Installation, Configuration, and Usage Guide for Customers - 4.01.03.01, 4.01.07.00

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Teradata Query Service
Release Number
4.01.03.01, 4.01.07.00
Published
September 2023
Language
English (United States)
Last Update
2024-02-16
dita:mapPath
qla1666844235399.ditamap
dita:ditavalPath
ft:empty
dita:id
enn1467244092237
Product Category
Analytical Ecosystem

The following example uses the JQuery JavaScript library within a browser to query a Teradata Database and display the results on a web page. The web page displays the databases on the target Teradata System ordered by size. Though only displayed as a JSON result set in the example, the results could be displayed visually using a library such as D3.

<!DOCTYPE html>
<html>
<head>
<title>Query Service Example Using JQuery</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(function()
{
    var username = 'myuser';
    var password = 'mypassword';
    var sql = "SELECT TRIM(DatabaseName) AS \"Database\", PermSpace  AS \"Size\" FROM dbc.databases ORDER BY 2 DESC;"
    var url = "http://sdl57120.labs.teradata.com:1080/systems/mysystem/queries"

    $.ajax({
        type: "POST",
        url: url,
        contentType: "application/json",
        headers: {
            'Accept' : "application/vnd.com.teradata.rest-v1.0+json",
            'Authorization': 'Basic ' + btoa(username + ':' + password)
        },
        data: JSON.stringify({
            query: sql,
            format: 'object'
        }),
    }).done(function (data)
    {
        $("#message").text("RESULTS: SUCCESS");
        $("#result").append(JSON.stringify(data, null, 2));
    }).fail (function (error)
    {
        $("#message").text("RESULTS: ERROR, HTTP Code: " + error.status);
        $("#result").append(JSON.stringify(error.responseJSON, null, 2));
    })

    $("#query").text("Executing query: " + sql);
})
</script> 
</head>
<body>
<div>
  <span id="query"></span>
</div>
<div>
  <span id="message"></span>
  <pre id="result"></pre>
</div>
</body>
</html>