0
votes

I have created an HTTP adapter to call a procedure to add 2 numbers. Its a basic program to send input and display the output. Below is my code.

HTML:

First Integer: <input type="text" id="firstint"/>
Second Integer: <input type="text" id="secondint"/>
<input type="button" onclick="submitInput()" value="Submit"/>

DemoHTTPAdapter-impl.js :

    function submitInput() {
        var invocationData = {
            adapter : 'DemoHTTPAdapter',
            procedure : "getDemoAdd",
            parameters : [$('#firstint').val(),$('#secondint').val()]
        };

        var options = {
            onSuccess : success,
            onFailure : failure
        };

        WL.Client.invokeProcedure(invocationData, options);
    }

function getDemoAdd(firstint, lastint) {
    return {
        result : com.ibm.demo.DemoAdd.addTwoInteger(firstint,secondint)
    }
}

The procedure is invoked through a java code as below in the DemoAdd.java:

package com.ibm.demo;
import java.util.logging.Logger;

public class DemoAdd {  
    private static final Logger logger = Logger.getLogger(DemoAdd.class.getName());

public static int addTwoInteger(int afirstInt, int aSecondInt) {        
    logger.info("Add Method Invoked with Parameter " + afirstInt + " & " + aSecondInt);
    return (afirstInt + aSecondInt);
}

I am getting an error when I run the html.

submitInput() not defined.

whereas I have already defined the submitInput() method.

1

1 Answers

0
votes

You've placed your submitInput function inside the adapter implementation file (DemoHTTPAdapter-impl.js) instead of in your common\js\main.js file.

You need to differentiate between the adapter scope, which resides on the Worklight Server and thus you cannot call it directly, to the application scope which resides in the application itself.

Because they are of different scopes, the parser tries to find submitInput, but cannot find it.

The application's JavaScript needs to call the Worklight framework in order to send the request to the server to invoke the adapter.

Move the following code to your main.js:

function submitInput() {
    var invocationData = {
        adapter : 'DemoHTTPAdapter',
        procedure : "getDemoAdd",
        parameters : [$('#firstint').val(),$('#secondint').val()]
    };

    var options = {
        onSuccess : success,
        onFailure : failure
    };

    WL.Client.invokeProcedure(invocationData, options);
}