cancel
Showing results for 
Search instead for 
Did you mean: 

CORS does not work for ODataModel (but same service works as JSONModel)

former_member182374
Active Contributor
0 Kudos

Hello Experts

,

My SAPUI5 application server is a SAP J2EE engine and my Gateway server is an ABAP server (there is SSO between the two servers).

I'm trying to run a service as JSONModel and ODataModel:

1) JSON Model

    var oModel = new sap.ui.model.json.JSONModel();

    jQuery.ajax({

        url: "<server>:<port>sap/opu/odata/sap/ZOMRI_PRJ2_SRV/ConnectionDetailsSet(RequestText='Omri')?$format=json",

        dataType: "json",

        xhrFields: {

            withCredentials: true

        },

        success: function(data, textStatus, jqXHR) {

            oModel.setData({data: data});

           

            console.log(data.d.RequestText);

            console.log(data.d.ResponseText);

            console.log(data.d.ResponseEchoText);

        },

        error: function(jqXHR, textStatus, errorThrown) {

            console.error("Error --> " + errorThrown);

        }

    });  

   

in the ABAP side (Gateway), I added the following headers in DPC_EXT class:

ls_header-name = 'Access-Control-Allow-Origin'.

ls_header-value = '<server>:<port>'.

/iwbep/if_mgw_conv_srv_runtime~set_header( ls_header ).

ls_header-name = 'Access-Control-Allow-Credentials'.

ls_header-value = 'true'.

/iwbep/if_mgw_conv_srv_runtime~set_header( ls_header ).

This works and I'm getting the result data.

2) Trying to run the service as ODataModel (or to be exact create the model without invoking and method)

    var oModel2 = new sap.ui.model.odata.ODataModel(

        "http://<server>:<port>/sap/opu/odata/sap/ZOMRI_PRJ2_SRV/",

        true, // bJSON

        null, // sUser

        null, //sPassword

        true, // mHeaders

        false, //bTokenHandling

        true //bWithCredentials

    );                

   

    Doesn't work - I'm getting 401 Unauthorized error

   

   

SAP UI Version is 1.16.6

I assumed that setting 'bWithCredentials' to true will work with ODataModel because it's working on JSONModel.

How can I solve it?

Regards,

Omri

Accepted Solutions (1)

Accepted Solutions (1)

former_member182374
Active Contributor
0 Kudos

Solved it by using APACHE as reverse proxy.

0 Kudos

Can you please tell me step by step what needs to be done.

former_member182374
Active Contributor
0 Kudos

Hi,

Our BASIS team configured Apache so I don't have the procedure )-:

Omri

0 Kudos

Hi,

Detail explanation of the issue-

I have 2 laptops in both laptops I am using eclipse Mars.

In 1 laptop my code is working fine when I use Internet Explorer (but not in chrome and mozilla). Here I have windows 7.

In another laptop I am getting 'no data' for all browser even in IE. Here I am using Windows 10.

While calling Odata service getting 401 unauthorized issue.

So why same code is working in 1 laptop in IE but not in another laptop ?

var oModel = new sap.ui.model.odata.ODataModel("http://220.227.93.78:8000/sap/opu/odata/SAP/ZBG_FLIGHT_SRV", false, "Username", "Password");

Answers (1)

Answers (1)

kammaje_cis
Active Contributor
0 Kudos

IMO, '401' is different than the CORS issue. It might be because of sending " true //bWithCredentials" but null for userid/password.

former_member182374
Active Contributor
0 Kudos

Thanks.

For the JSON Model I didn't enter any user/password (the logon ticket worked).

Does it mean that ODataModel is not supporting Logon Tickets (unlike JSON Model), after all it's the same gateway system...

Hard coding user & password is not an option for production.

Regards,

Omri

kammaje_cis
Active Contributor
0 Kudos

I would try

   var oModel2 = new sap.ui.model.odata.ODataModel("http://<server>:port>/sap/opu/odata/sap/ZOMRI_PRJ2_SRV")

without supplying any other optional parameters.

former_member182374
Active Contributor
0 Kudos

Hi,

I tried to write a single line of code:


var oModel2 = new sap.ui.model.odata.ODataModel("http://<server>:<port>/sap/opu/odata/sap/ZOMRI_PRJ2_SRV");

This is the console output:


2014-08-19 08:46:31 Device API logging initialized - DEVICE sap-ui-core.js:41

2014-08-19 08:46:31 registerResourcePath ('', '/sapui5/resources/') -  sap.ui.ModuleSystem sap-ui-core.js:77

2014-08-19 08:46:31 URL prefixes set to: -  sap.ui.ModuleSystem sap-ui-core.js:77

2014-08-19 08:46:31   (default) : /sapui5/resources/ -  sap.ui.ModuleSystem sap-ui-core.js:77

2

OPTIONS http://<server>:<port>/sap/opu/odata/sap/ZOMRI_PRJ2_SRV/$metadata 401 (Unauthorized) datajs.js:17

OPTIONS http://<server>:<port>/sap/opu/odata/sap/ZOMRI_PRJ2_SRV/$metadata 401 (Unauthorized) datajs.js:17

XMLHttpRequest cannot load http://<server>:<port>/sap/opu/odata/sap/ZOMRI_PRJ2_SRV/$metadata. Origin http://<server2>:<port2> is not allowed by Access-Control-Allow-Origin. datajs.js:17

2014-08-19 08:46:32 The following problem occurred: A network error occurred. -   sap-ui-core.js:77

u sap-ui-core.js:77

fatal sap-ui-core.js:77

sap.ui.model.odata.ODataModel._handleError ODataModel.js:35

b ODataMetadata.js:7

o.request datajs.js:17

o.read datajs.js:17

sap.ui.model.odata.ODataMetadata._loadMetadata ODataMetadata.js:7

sap.ui.base.Object.extend.constructor ODataMetadata.js:6

sap.ui.model.Model.extend.constructor ODataModel.js:6

o sap-ui-core.js:117

(anonymous function) index.html:60

Regards,

Omri