JavaScript no Outsystems Web

JavaScript no Outsystems Web

  

Olá,

Estou enfrentando um problema, preciso usar uma função JavaScript numa aplicação Web. 

Para entender melhor, darei uma breve introdução sobre o projeto. Meu cliente me forneceu uma função JavaScript que chama uma url e insere seus parâmetros. Tentei diversas maneiras de replicar essa função no outsystems, todas sem sucesso. A função é a seguinte :

-----------------------------------------------------------------------------------------------------------------------------------------

const FormData = require('form-data');
const request = require('request');
const extend = require('extend');
const fs = require('fs');
const path = require('path');


function login() {
    var r = request.post('https://treinamento.icolabora.com.br/api/auth/login',
        {
            form: {
                username: *******,
                password: ********
            }
        },
        function (error, response, body) {
            if (!error && response.statusCode == 200) {
                var responseBody = JSON.parse(body);
                if (responseBody.success) {
                var headers = {
                    headers: {Cookie: response.headers['set-cookie']}
                };
               
                console.log('conectado com sucesso');

                }
            } else {
                console.log('ERRO NO LOGIN');
                console.log(error);
                console.log('HTTP STATUS: ' + response.statusCode);
            }
        }
    );
}
 
login();

-----------------------------------------------------------------------------------------------------------------------------------------

Como chamar essa função no Outsystems?Sendo que meu ambiente é desenvolvido exclusivamente em Java.


Obrigado.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Hello,

I'm facing a problem, I need to use a JavaScript function in a web application.

To understand better, I will give a brief introduction about the project. My client 
provided me a JavaScript function that calls a url and inserts its parameters. I tried several ways to replicate this function in outsystems, all to no avail. The function 
is as follows:

-------------------------------------------------- -------------------------------------------------- -------------------------------------

const FormData = require ('form-data');
const request = require ('request');
const extend = require ('extend');
const fs = require ('fs');
const path = require ('path');


function login () {
    var r = request.post ('https://treinamento.icolabora.com.br/api/auth/login',
        {
            form: {
                username: *******,
                password: ********
            }
        },
        function (error, response, body) {
            if (! error && response.statusCode == 200) {
                var responseBody = JSON.parse (body);
                if (responseBody.success) {
                var headers = {
                    headers: {Cookie: response.headers ['set-cookie']}
                };
               
                console.log ('successfully connected');

                }
            } else {
                console.log ('ERROR NO LOGIN');
                console.log (error);
                console.log ('HTTP STATUS:' + response.statusCode);
            }
        }
    );
}
 
Login();

-------------------------------------------------- -------------------------------------------------- -------------------------------------

How to call this function in Outsystems? My environment is developed exclusively in 
Java.



Thank you.

Hello Jose,

Please, use only English in the forum :)

At which moment you want to execute the "login" function?
As soon as the browser starts to rendering the page?

If so, select the page where you want to run it and add this code to the JavaScript ptoperty of the page.

If you want to run the Login only when the user do something (like click a button), put the function code inthe JavaScript of the page and than call the function from the action of the button (you can run a RunJavaScript action with the Login(); call). It is in the HTTPRequestHandler extension.

There are other ways, like running directly from buttons or links or divs (with onclick extended parameter defined) etc.

Cheers,
Eduardo Jauch

Eduardo Jauch wrote:

Hello Jose,

Please, use only English in the forum :)

At which moment you want to execute the "login" function?
As soon as the browser starts to rendering the page?

If so, select the page where you want to run it and add this code to the JavaScript ptoperty of the page.

If you want to run the Login only when the user do something (like click a button), put the function code inthe JavaScript of the page and than call the function from the action of the button (you can run a RunJavaScript action with the Login(); call). It is in the HTTPRequestHandler extension.

There are other ways, like running directly from buttons or links or divs (with onclick extended parameter defined) etc.

Cheers,
Eduardo Jauch


Thank you for replying so quickly and sorry abou the text in portugues, i didn'n know .


Well, about the problem, i want to use it in a button, like a action. So i put the function in JavaScript property. In the same screen i put the button calling a action.  And this action contains the RunJavaScript. 

But i have two doubts :

1. In the JavaScript property , i put the function like this:


Is correct?

 2. In te "OK" action, i call a function like this

But, what i set in Script?

Thanks so much for the help .

Hello,

You can do like this bellow.

Put this part of the code in the "JavaScript" property of the page:


const FormData = require ('form-data');
const request = require ('request');
const extend = require ('extend');
const fs = require ('fs');
const path = require ('path');


function login (username_value, password_value) {
    var r = request.post ('https://treinamento.icolabora.com.br/api/auth/login',
        {
            form: {
                username: username_value,
                password: password_value
            }
        },
        function (error, response, body) {
            if (! error && response.statusCode == 200) {
                var responseBody = JSON.parse (body);
                if (responseBody.success) {
                var headers = {
                    headers: {Cookie: response.headers ['set-cookie']}
                };
               
                console.log ('successfully connected');

                }
            } else {
                console.log ('ERROR NO LOGIN');
                console.log (error);
                console.log ('HTTP STATUS:' + response.statusCode);
            }
        }
    );
}


Than, in the screen action called by the Button (remove the onclick extended property), call the RunJavaScript with the following script:

"login(" + login + "," + password + ");"

"login" and "password" are local variables to the screen action, taht you fill up with the correct values before the RunJavaScript action.

This should do the trick (I think...).

Cheers,
Eduardo Jauch

Hi José,

I believe that call this login api will be a quite unsafe. You will expose api, username and password directly on the request. Is not a good idea.

I undestand that your login api is a post rest call using a form-data to pass username and password. 

I found this thread to help you to create call you api directly on studio , using the native consume of Rest services: https://www.outsystems.com/forums/discussion/17055/rest-api-multipart-form-data/


Best regards

Fabio Fantato