jQuery [examples]

// create a record

// declare a function that uses jQuery.
//
// url:              the odata URL
// data:             the data "object"
// callback:         a callback "function" called for async operation
// async:            true if async call false otherwise
// args              an args "object" that will be passed to your "callback" function
//
function create_data(url, data, callback, async, args)
{
       if (typeof callback !== 'function') async = false;
       else if (typeof async !== 'boolean') async = true;
 
       var request = $.ajax({
              url: url,
              contentType: "application/json",
              dataType: 'json',
              async: async,
              cache: false,
              type: "POST",
              data: data,
              mycallback: callback,
              myargs: args,
              success: function (response, textStatus, jqXHR) {
                     if (typeof this.mycallback === 'function')
                            this.mycallback(true, response, this.myargs);
              },
              error: function (response, textStatus, jqXHR) {
                     if (typeof this.mycallback === 'function')
                            this.mycallback(false, textStatus, this.myargs);
              }
       });
 
       if (!async)
       {
              return request.status == 200;
       }
       else
       {
              return false;
       }
};
var result = create_data(
       "/odata/southwind/customers",
       { "customerid": "ABCD",
       ...
       }
);

// read records

function read_data(url, callback, async, args)
{
       if (typeof callback !== 'function') async = false;
       else if (typeof async !== ‘boolean’) async = true;
 
       $.ajax({
              url: url,
              type: "GET",
              dataType: 'json',
              contentType: "application/json",
              mycallback: callback,
              async: async,
              cache: false,
              myargs: args,
              success: function (response, textStatus, jqXHR) {
                     if (typeof this.mycallback === 'function')
                            this.mycallback(true, response, myargs);
              },
              error: function (response, textStatus, jqXHR) {
                     if (typeof this.mycallback === 'function')
                            this.mycallback(false, textStatus, myargs);
              }
       });
};

// update a record

function update_data(url, data, callback, async, args)
{
       if (typeof callback !== 'function') async = false;
       else if (typeof async !== ‘boolean’) async = true;
 
       var request = $.ajax({
              url: url,
              async: async,
              cache: false,
              type: "PUT",
              dataType: 'json',
              contentType: "application/json",
              data: data,
              mycallback: callback,
              myargs: args,
              success: function (response, textStatus, jqXHR) {
                     if (typeof this.mycallback === 'function')
                            this.mycallback(true, response, this.myargs);
              },
              error: function (response, textStatus, jqXHR) {
                     if (typeof this.mycallback === 'function')
                     this.mycallback(false, textStatus, this.myargs);
              }
       });
 
       if (!async)
       {
              return request.status == 200;
       }
       else
       {
              return false;
       }
};

// delete a record

function delete_data(url, data, callback, async, args)
{
       if (typeof callback !== 'function') async = false;
       else if (typeof async !== 'boolean') async = true;
       var request = $.ajax({
              url: url,
              async: async,
              cache: false,
              type: "DELETE",
              dataType: 'json',
              contentType: "application/json",
              data: data,
              mycallback: callback,
              myargs: args,
              success: function (response, textStatus, jqXHR) {
                     if (typeof this.mycallback === 'function')
                            this.mycallback(true, response, this.myargs);
              },
              error: function (response, textStatus, jqXHR) {
                     if (typeof this.mycallback === 'function')
                            this.mycallback(false, textStatus, this.myargs);
              }
       });
 
       if (!async)
       {
              return request.status == 200;
       }
       else
       {
              return false;
       }
};

A custom .rsp web service to retrieve JSON data using jQuery

The Lianja SQL engine has many extensions to standard SQL SELECT to provide a simpler and quicker way for generating JSON from a SQL SELECT.

Client-side jQuery call

function get_data(callback, async, args)
{
       if (typeof callback !== 'function') async = false;
       else if (typeof async !== ‘boolean’) async = true;
 
       var jqXHR = $.ajax({
              url: get_data.rsp,
              type: "GET",
              dataType: 'json',
              contentType: "application/json",
              mycallback: callback,
              async: async,
              cache: false,
              myargs: args,
              success: function (response, textStatus, jqXHR) {
                     if (typeof this.mycallback === 'function')
                            this.mycallback(true, response, this.myargs);
              },
              error: function (response, textStatus, jqXHR) {
                     if (typeof this.mycallback === 'function')
                            this.mycallback(false, textStatus, this.myargs);
              }
       });
};
 
get_data(
       function(status, json_data)
       {
              if (status)
              {
                     // json_data contains an array of json objects
              }
              else
              {
                     // oops an error occurred
              }
       },
       true,
       {}
};

Server-side get_data.rsp web service to query data and send back as JSON

open database southwind
select * from orders into arrayofobjects console where customerid="wilmk"
// alternatively we don't need to open the database and can do this as a single command
select * from southwind!orders into arrayofobjects console limit 1,2

 https://www.lianja.com/doc/index.php/Working_with_JSON_and_JQL


 

Advertisements