-
Login to using callback
This is an example of using DataConnect with callbacks. Callbacks are function the is defined in a specified format. The function gotdata(jdata) is such a function. It has one parameter jdata and this is a javascript object with the structure returned from the server in a the jdata.data object. The jdata.result should have the string value 'ok' if all went well and error or failed if not. If you have error as a result, you will also have the value errorcode and error telling you what went wrong.
... <pre id="returndata" style="background-color: antiquewhite;"></pre> ... <script src="https://global.assets/js/2imply.js"</script> <script> let dataconnect = new DataConnect("data"); // set up connection to the default data object function gotdata(jdata){ if ('cmd' in jdata && jdata.cmd == 'login' && 'data' in jdata){ if (jdata.data.isloggedin){ isloggedin = true; $("#message").html("Logged in"); const thelogindialog = document.getElementById('logindialog'); const logindialog = bootstrap.Modal.getInstance(thelogindialog); logindialog.hide(); } else { $("#loginmsg").html("Wrong user name or password, try again."); } } else $("#loginmsg").html("no result from server, try again."); $("#returndata").html(JSON.stringify(jdata, null, 2)); } function dologin(){ let un = $("#username").val(); let pw = $("#password").val(); dataconnect.login(un,pw,gotdata); } function dogetdata(){ dataconnect.getrecord("Address",'Name="U*"',0); } </script>
As you can see a lot easier than doing this manually. Both dologin and dogetdata will display the resulting json coming back from the server in a pre with id returndata. The dologin will take user name and password from the html fields.