Loading OData without Table or List object in SAPUI5

僤鯓⒐⒋嵵緔 提交于 2020-02-29 05:03:49

问题


I have 2 weeks looking for an example to understand how the OData works. I have defined in the Manifest.json my url with the OData service

{
"_version"  : "1.7.0",
"sap.app"   : {
    "id"            : "test",
    "type"          : "application",
    "i18n"          : "i18n/i18n.properties",
    "applicationVersion": {
        "version"       : "1.0.0"
    },
    "title"         : "{{appTitle}}",
    "description"   : "{{appDescription}}",
    "sourceTemplate": {
        "id"            : "servicecatalog.connectivityComponent",
        "version"       : "0.0.0"
    },
    "dataSources"   : {
        "Test"  : {
            "uri"           : "/sap/opu/odata/sap/ZMY_SERVICE_SRV/",
            "type"          : "OData",
            "settings"      : {
                "odataVersion"  : "2.0",
                "localUri"      : "localService/metadata.xml"
            }
        }
    }
}..
    "models": {
        "i18n": {
            "type": "sap.ui.model.resource.ResourceModel",
            "settings": {
                "bundleName": "test.i18n.i18n"
            }
        },
        "Test": {
            "type": "sap.ui.model.odata.v2.ODataModel",
            "settings": {
                "defaultOperationMode": "Server",
                "defaultBindingMode": "TwoWay",
                "defaultCountMode": "None"
            },
            "dataSource": "Test"
        },

and in my Component.js within the Init method:

init: function() {
        // call the base component's init function
        UIComponent.prototype.init.apply(this, arguments);
        // create the views based on the url/hash
        this.getRouter().initialize();
        // set the device model
        this.setModel(models.createDeviceModel(), "device");


        var sServiceUrl = this.getMetadata().getManifestEntry("sap.app").dataSources["Test"].uri;
        var oModel = new sap.ui.model.odata.v2.ODataModel(sServiceUrl);
        this.setModel(sabModel, "/Test");
        sabModel.read(sServiceUrl, "Test");
    }

I don´t want to use a Table or a List to load the OData from Backend. I want to load the information "manually" and based on what I got from the Backend, I want to navigate to one or another View.

Debugging the result in the navigator I see the following error: Console

Checking the Error Log I can see: Error Log

If I test the service in Backend works fine: OData Service

I got:

<?xml version="1.0"?><feed xml:base="http://Myserver:8000/sap/opu/odata/sap/ZMY_SERVICE_SRV/" 
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<id>http://Myserver:8000/sap/opu/odata/sap/ZMY_SERVICE_SRV/TestSet</id>
<title type="text">TestSet</title>
<updated>2017-10-23T20:37:55Z</updated>
-<author>
    <name/>
</author>
<link title="TestSet" rel="self" href="TestSet"/>
-<entry>
    <id>http://Myserver:8000/sap/opu/odata/sap/ZMY_SERVICE_SRV/TestSet('1')</id>
    <title type="text">TestSet('1')</title>
    <updated>2017-10-23T20:37:55Z</updated>
    <category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="ZMY_SERVICE_SRV.Test"/>
    <link title="Test" rel="self" href="TestSet('1')"/>
    -<content type="application/xml">
        -<m:properties xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
            xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
            <d:Pernr>1</d:Pernr>
            <d:Nachn>TestUser</d:Nachn>
            <d:Vorna>UserTest</d:Vorna>
            <d:SavingDate m:null="true"/>
            <d:Option/>
            <d:MsgType/>
            <d:MsgNumb/>
        </m:properties>
    </content>
</entry>

Thank you for your help!!! Any input is more than welcome!!


回答1:


Read the API documentation about "read" method: https://sapui5.netweaver.ondemand.com/#/api/sap.ui.model.odata.v2.ODataModel

You should not specify the service URL in read method, because it automatically gets concatenated with "sPath", which you pass as a first argument. You should use the name of entityset (is defined in $metadata) you want to read starting with slash "/".




回答2:


try this

var oModel = this.getModel("Test");//get the model
oModel.read("/TestSet", {
    method: "GET",
    success: function(data) {
        alert(JSON.stringify(data));
    },
    error: function() {

    }
});


来源:https://stackoverflow.com/questions/46898423/loading-odata-without-table-or-list-object-in-sapui5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!