Skip to Content
1
Jul 08 at 08:02 PM

How to Access Data from OData-Service in SAPUI5

98 Views

Hello everyone!
I am new to SAPUI5 Development and starting to learn step by step.
For now I have created a litle OData-Service in which a query is made in the backend , that returns a string, that says "successful" or "not successful".
I would like to access this string and displaying it in a MessageToast or something like that.
The string in the <d:Result>-Tag is my target (green marked). I made a screenshot when opening the URL of the OData-Service:


For now I was able to access the OData-Service via an ajax-Call but now I am struggeling in getting the result-string from the XML.
I saw in the some forums that I should first bind the data to a model and then access my target from there...

The ajax-Call actually worked as I can see the <d:Result>-Tag and everything else when pressing F12 in the browser.

I will just show you what I was trying to do, maybe someone could help me.

Best regards
Daniel

sap.ui.define([
    'sap/m/MessageToast',
    'sap/ui/core/mvc/Controller',
    "sap/ui/model/xml/XMLModel"
],function(MessageToast, Controller, XMLModel) {
    "use strict";

    return Controller.extend("sap.df.demo.odatatest.controller.Request", {

        onShowMessageToast: function(oEvent) {

            var sUrl = "/sap/opu/odata/sap/ZTEST_ODATA_SRV/ZZ_TEST_ODATASet('1210007678')"
            var that = this;
            //Make a Call using AJAX
            $.ajax({
                type: "GET",
                url: sUrl,
                username: "*******",
                passwort: "*******",
                success: function (data){
                    //step 1: Define Model and step 2: Load or Set Data to Model
                    var oModel = new XMLModel(data);
                    //step 3: Model set to View
                    that.getView().setModel(oModel,"testModel");

                    console.log(data);
                },
                error: function(){
                    console.log(error)
                }
            });

            var output = this.getView().getModel("testModel").getElementsByTagName("d:Result");

            MessageToast.show(output);

        }
    });
});