将文本输入值传递给IBM Worklight适配器

时间:2014-04-11 09:20:50

标签: javascript ibm-mobilefirst worklight-adapters

  1. 以下代码是否接受来自客户端的值并传递给适配器?
  2. 我的JS函数会验证db?
  3. 中是否存在用户名和密码

    HTML

    <form action="index.html" onsubmit="return(login())">
        UserName:<input type="text" name="uname" id="username"><br>
        Password:<input type="password" name="pass" id="password"><br>
        <input type="submit" value="Login">
    </form>
    

    的JavaScript

    function login() {
        var invocationData = {
            adapter : 'sq',
            procedure : "validateLogin",
            parameters : [$('#username').val(),$('#password').val()]
        };
    
        var options = {
            onSuccess : success,
            onFailure : failure
        };
    
        WL.Client.invokeProcedure(invocationData, options);
    }
    
    function success() {
        var allNames = new String();
        var allpass = new String();
    
        for (var i = 0; i < response.invocationResult.resultSet.length; i++) {
            allNames+= response.invocationResult.resultSet[i].name;
            allpass+= response.invocationResult.resultSet[i].pass;
        }
    
        var a=new String();
        var b=new String();
        a=JSON.stringify(allNames);
        b=JSON.stringify(allpass);
    
        if (a==$('#username').val() && b==$('#password').val() ){
            return true;
        } else {
            return false;
        }
    }
    
    function failure() {
        return false;
    }
    

    适配器程序

    var query = WL.Server.createSQLStatement(
        "select name,pass from userreg where name=? and pass=?");
    
    function validateLogin(username,password) {
        return WL.Server.invokeSQLStatement({
            preparedStatement : query,
            parameters : [username,password]
        });
    }
    

1 个答案:

答案 0 :(得分:1)

<强> HTML:

<form id="frmId" onsubmit="return(login())">
    UserName:<input type="text" name="uname" id="username"><br>
    Password:<input type="password" name="pass" id="password"><br>
    <input type="submit" value="Login">
</form>

成功功能:

function success() {
    var allNames = new String();
    var allpass = new String();

    for (var i = 0; i < response.invocationResult.resultSet.length; i++) {
        allNames+= response.invocationResult.resultSet[i].name;
        allpass+= response.invocationResult.resultSet[i].pass;
    }

    var a=new String();
    var b=new String();
    a=JSON.stringify(allNames);
    b=JSON.stringify(allpass);

    if (a==$('#username').val() && b==$('#password').val() ){
        $("#frmId").attr('action', URL of the page you wants to redirect to);
        return true;
    } else {
        return false;
    }
}