cancel
Showing results for 
Search instead for 
Did you mean: 

odata.ODataModel vs json.JSONModel and POST

Former Member
0 Kudos

Good day, dear experts!

I need to send the data to the application in Jason format:

           var sendInfo = {};

           sendInfo.TEXT1 = "text data1";

           sendInfo.TEXT2 = "text data2";

           sendInfo.TEXT3 = "text data3";

and

  sendInfo.FILECONTENT = content of file;

for sending data I need to use only POST

and the application must return MESSAGE

what the model I need to use?

sap.ui.model.odata.ODataModel();

or

sap.ui.model.json.JSONModel();

I can not  read a MESSAGE if I use odata.ODataModel(); and POST

oModel.read("MESSAGE", function(){

         alert("MESSAGE");

});

not work.

if I use

json.JSONModel (); and try to use the POST, data is not  sending.

Accepted Solutions (1)

Accepted Solutions (1)

santhu_gowdaz
Active Contributor
0 Kudos

Hi Nataly,

              Model not required for post the data to back end.

use OData.request() or OData.read() method.

sample code:

OData.request

        ({

            requestUri: "YOUR_URL",

            method: "GET",

            headers:

  { 

                    "X-Requested-With": "XMLHttpRequest",                      

                    "Content-Type": "application/atom+xml",

                    "DataServiceVersion": "2.0",

                    "Accept": "application/atom+xml,application/atomsvc+xml,application/xml",

                    "X-CSRF-Token":fetch  

                },

          

        },                 

        function (data, response){

            header_xcsrf_token = response.headers['x-csrf-token'];

            OData.request

            ({

              

                requestUri: "YOUR_URL",

                method: "POST",

                headers:

                { 

                    "X-Requested-With": "XMLHttpRequest",                      

                    "Content-Type": "application/atom+xml",

                    "DataServiceVersion": "2.0",

                    "Accept": "application/atom+xml,application/atomsvc+xml,application/xml",

                    "X-CSRF-Token": header_xcsrf_token  

                },

              

                data: requestORderHeader

//requestORderHeader is an array(Holding the data to be post).

              

            },

            function (data, response)

            {

           

               console.log(" Created Successfully");

            },

            function (err)

            {

              

                console.log(err);

            }

            );

        },

        function (err) {

            var request = err.request; // the request that was sent.

            var response = err.response; // the response that was received.

console.log("Error in Get -- Request "+request+" Response "+response);

        }  

        );

if u need any help, Please let me know.

regards,

Santhosh Gowda

Former Member
0 Kudos

Hi, Santhosh Gowda.

thank you!

It is work:

......

       var sendInfo = {};

           sendInfo.PRIORITET = encodeURIComponent(proritet);

           sendInfo.TEXT_SHORT = encodeURIComponent(textShort);

           sendInfo.TEXT_SHORT = encodeURIComponent(textFull);

           sendInfo.MESSAGE = "";

          //I I do not know how to send a file to the application. I am try to use FileUploader.

          //it will be a separate odata
          // sendInfo.FILENAME


      // POST DATA //

       var oUrl='/CreateIncidents';

       var oModel = new sap.ui.model.odata.ODataModel(oController.oGatewayURL);

           oModel.create(

             oUrl,

             sendInfo,

             null,

             function(oData, oResponse){

                 var dataget = JSON.stringify(oData,["MESSAGE"]);

                 sap.ui.commons.MessageBox.show(JSON.parse(dataget)["MESSAGE"], "SUCCESS", "yes" );

             },

             function(oData){

                 sap.ui.commons.MessageBox.show("Err "+oData.response.body +". Upload filed.", "ERROR", "Err");

             }

           );



        }

     catch (e) {

        sap.ui.commons.MessageBox.show('err . '+e.message, "ERROR", "err" );

        }

  }
.....

Many thanks to all the experts for answers!

Answers (2)

Answers (2)

former_member182372
Active Contributor
0 Kudos

In that case you dont have to use model, use jQuery.sap.sjax for sync calls or jQuery.ajax for async

or even more simplified

jQuery.sap.syncPost = function syncPost(sUrl, data, sDataType)

that will return result (MESSAGE in your case)

Former Member
0 Kudos

Hi,

Use sap.ui.model.odata.ODataModel to send the data to back end

below is the example


var sPath = "/Products"; //path to be sent(filter)

         var odata = {

          ID:12,

      Rating:2.6,

      Price:4

      };

      

         var mparameters = {

      context:{},

      success:function(oSuccess){

      console.log("success");

      },

      error:function(oError){

      console.log("failure");

      },

      merge:false,

      };

         omodel.update(sPath, odata,mparameters);

//this is for updating the data

for creating the data


var sPath = "/Products";    //filters

      var odata = {

      ID:11,

      Name:"BreadButter",

      Description:"Whole",

      ReleaseDate:"1991-01-01T00:00:00",

      DiscontinuedDate:null,

      Rating:4.3,

      Price:3,

    

    

      };

      var mparameters = {

      context:{},

      success:function(oSuccess){

      console.log("success");

      },

      error:function(oError){

      console.log("failure");

      },

      merge:false,

      };

    

      omodel.create(sPath,odata,mparameters);

regards,

Nagarjun