IBM Worklight - How to parse adapter response?

2019-09-09 08:26发布

问题:

This is my stored procedure:

CREATE PROCEDURE PROC()
BEGIN
SELECT * FROM TABLENAME;
END//

This is my unction to call stored procedure using SQL adapter:

function callStored() {
    return WL.Server.invokeSQLStoredProcedure({
        procedure : "proc",
        parameters : []
    });
}

This is the invocationResult:

{
   "isSuccessful": true,
   "resultSet": [
      {
         "name": "a",
         "pass": "123",
         "time_stamp": "2014-04-07T10:13:17.000Z"
      },
      {
         "name": "chetan",
         "pass": "123456",
         "time_stamp": "2014-04-07T10:13:34.000Z"
      },
      {
         "name": "dileep",
         "pass": "456321",
         "time_stamp": "2014-04-07T10:13:54.000Z"
      },
      {
         "name": "bnc",
         "pass": "654321",
         "time_stamp": "2014-04-07T10:19:37.000Z"
      }
   ]
}

I need to parse this and display or alert the values of name, pass and time_stamp.

How do I accomplish this?

回答1:

In your application JavaScript (common\js\main.js), you can have something like the following. In the below code an alert will be displayed with the values of the name key from the resultSet.

You can also take a look here: use resultset returned by WL.Server.invokeSQLStatement within sql adapter procedure

function wlCommonInit() {
    invokeAdapter();
}

function invokeAdapter() {
    var invocationData = {
        adapter: "your-adapter-name",
        procedure: "callStored",
        parameters: []
    };

    WL.Client.invokeProcedure (invocationData, {
        onSuccess: invocationSuccess,
        onFailure: invocationFailure}
    );
}

function invocationSuccess(response) {
    var i,
        resultSet = response.invocationResult.resultSet;
        namesArray = [];

    for (i = 0; i < resultSet.length; i++) {
        namesArray[i] = resultSet[i].name;
    }
    alert (JSON.stringify(namesArray));
}

function invocationFailure(response) {
   alert (response);
}

You have already asked this here: ibm worklight stored procedure
Why did you not follow through the documentation and learned how to write the above?

Please read the documentation!



回答2:

Please read the "Invoking adapter procedures from client applications" and its Exercise and code sample in the Getting started with IBM Worklight page.

function wlCommonInit(){
    getUsersInfo();
}

function getUsersInfo(){
    var invocationData = {
        adapter : 'YOUR_ADAPTER',
        procedure : 'YOUR_PROCEDURE',
        parameters : []
    };

    WL.Client.invokeProcedure(invocationData,{
        onSuccess : getUsersInfoSuccess,
        onFailure : getUsersInfoFailure
    });
}

function getUsersInfoSuccess(result){
    if (result.invocationResult.Items.length > 0) {
        displayUsersInfo(result.invocationResult.Items);
    } else { 
        getUsersInfoFailure();
    }
}

function getUsersInfoFailure(result){
    alert("Cannot retrieve users info");
}

function displayUsersInfo(items){
    var i = 0, usersInfo = '';
    for (i = 0; i < items.length; i++) {
        usersInfo += ' name: ' + items[i].name;
        usersInfo += ' pass: ' + items[i].pass;
        usersInfo += ' time_stamp: ' + items[i].time_stamp;
    }
    alert(usersInfo);
}