Zum Inhalt

HTML/JS-Beispiel

Nachfolgendes Beispiel zeigt eine einfache Anwendung der grundlegenden crud-Funktionen (create, read, update, delete). Dieses Beispiel zeigt folgende Funktionen:

  • Login
  • neue Datensatz-ID anfordern
  • neuen Datensatz anlegen
  • Datensatz anhand einer ID lesen
  • Datensatz anhand der ID löschen

Notwendige Anpassungen

Die Login-Parameter und Felder müssen entsprechend der eigenen Umgebung angepasst werde.

Beispiel-Code

<html>
<head>
    <script type="text/javascript">

    var sDsURI = "https://localhost:4445/i/UniPlexDataservice/updjsr.php";
    var RequestId = 0;
    var userHdl = '';
    var dsIId = '';
    var lstDsUpd = '';
    var lstErrorCd = 0;

    function httpHandle(jObj, callback) {

        var http = new XMLHttpRequest();
        var url = sDsURI;
        var params = JSON.stringify(jObj);
        http.open('POST', url, true);

        http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

        http.onreadystatechange = function() {
            if(http.readyState == 4 && http.status == 200) {
                var result = JSON.parse(http.responseText);
                callback(result);
            }
        }
        http.send(params);

    }

    function getLogin() {

        var loginObj = {
            "method": "getLogin",
            "requestid": RequestId++,
            "param": {
                "user": 'admin',
                "pass": 'admin',
                "app": 'UniplexDataservice'
            }
        };

        httpHandle(loginObj, getLoginCB);

    }
    function getLoginCB(result) {
        userHdl = result.result.data.UpdJsrHdl;
    }

    function getNewID() {

        var getIIdObj  = {
            "method": "UpxLoadData",
            "requestid": RequestId++,
            "param": {
                "UpdJsrHdl": userHdl,
                "dst": "Debg",
                "iid": ""
            }
        };

        httpHandle(getIIdObj, getNewIIdCB);
    }
    function getNewIIdCB(result) {
        dsIId = result.result.data.iid;
    }

    function createNewDS() {

        var storeObj = {
            "method": "UpxStoreData",
            "requestid": RequestId++,
            "param": {
                "UpdJsrHdl": userHdl,
                "iid": dsIId,
                "dst": "Debg",
                "fields": {
                    "_frsNam": {
                        "v": "Homer"
                    },
                    "_lstNam": {
                        "v": "Simpson"
                    },
                    "_emlAdr": {
                        "v": "H.Simpson@springfield.town.com"
                    }
                }
            }
        }

        httpHandle(storeObj, createNewDSCB);
    }
    function createNewDSCB(result) {
        lstErrorCd = result.result.rc;
    }

    function readDS() {
        var readObj = {
            "method": "UpxLoadData",
            "requestid": RequestId++,
            "param": {
                "UpdJsrHdl": userHdl,
                "iid": dsIId
            }
        }
        httpHandle(readObj, readDSCB);
    }
    function readDSCB(result) {
        lstDsUpd = result.result.data.lupd;
    }

    function deleteDS() {
        var deleteObj = {
            "method": "UpxDeleteData",
            "requestid": RequestId++,
            "param": {
                "UpdJsrHdl": userHdl,
                "iid": dsIId,
                "lupd": lstDsUpd,
                "flags": "i",
                "dst": "Debg"
            }
        }

        httpHandle(deleteObj, deleteDSCB);
    }
    function deleteDSCB(result) {
        lstErrorCd = result.result.rc;
    }

    </script>
</head>
<body>
    <button onClick="getLogin();">Login</button>
    <button onClick="getNewID();">get new IID</button>
    <button onClick="createNewDS();">create new record</button>
    <button onClick="readDS();">read record</button>
    <button onClick="deleteDS();">delete created record</button>
</body>
</html>