IBM Worklight - 如何解析适配器响应?

时间:2014-04-09 07:37:41

标签: javascript json ibm-mobilefirst worklight-adapters

这是我的存储过程:

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

这是我使用SQL适配器调用存储过程的功能:

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

这是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"
      }
   ]
}

我需要解析此问题并显示或提醒namepasstime_stamp的值。

我如何做到这一点?

2 个答案:

答案 0 :(得分:0)

在您的应用程序JavaScript(common \ js \ main.js)中,您可以使用以下内容。在下面的代码中,将显示一个警报,其中包含来自resultSet的name键的值。

您还可以在此处查看: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);
}

您已在此处提出此问题:ibm worklight stored procedure
你为什么没有完成文档并学习如何编写上述内容?

请阅读文档!

答案 1 :(得分:0)

请阅读&#34; Invoking adapter procedures from client applications&#34;以及Exercise and code sample页面中的Getting started with IBM Worklight

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);
}