cancel
Showing results for 
Search instead for 
Did you mean: 

how to send json model via ajax?

Tula
Advisor
Advisor
0 Kudos

Hi all,

I am currently implementing a web app which store the user selected data in a JSON model and send it back to the server side, since the server side java program only acceptsJOSN encoded data. Therefore, I have to send the JSON model via ajax to java servlet and let java code further work with it. However, when doing so, I wonder how can I send JSON model in sapui5 via ajax?

should I directly assign model to the data configuration of ajax as follows

$.ajax({

  url: // url of servlet

  type: "POST",

  contentType: "application/json",

  dataType: "json",

  data:{

         jsonData: this.getView().getModel("model name")

     }

  })

or should I first convert the json model into corresponding string and then assign it?

$.ajax({

  url: // url of servlet

  type: "POST",

  contentType: "application/json",

  dataType: "json",

  data:{

         jsonData: this.getView().getModel("model name").getJSON() 

     }

  })

or is there any other way to send the json model via ajax to backend java program? Can anyone help me with this? Thank you very much:)

regards,

La

Accepted Solutions (1)

Accepted Solutions (1)

SergioG_TX
Active Contributor
0 Kudos

La,

in order to be able to send a JSON object, you need to serialize it using the JSON.stringify function.

// <yourObject> can be { propertyA: 123, propertyB: 'abc' };

data: JSON.stringify( <yourObject> ),

Tula
Advisor
Advisor
0 Kudos

Hi Sergio,

Many thanks for your quick answer:)

Since my data is already stored in sapui5 JSONModel, whose format is for instance as follows:

{    

"propertyA": 123,

"propertyB": "abc",

"propertyC": {

     "p1": "de",

     "p2": "us"

     }

}

do I still need to use JSON.stringify() method?

junwu
Active Contributor
0 Kudos

you have to,

SergioG_TX
Active Contributor
0 Kudos

yes you still need to use the stringify function... even if your json is in a model or not

Tula
Advisor
Advisor
0 Kudos

got it, thanks!

So it should be like this?


var oModel = this.getView().getModel("model name");


$.ajax({

  url: // url of servlet

  type: "POST",

  contentType: "application/json",

  dataType: "json",

  data: {

         jsonData: JSON.stringify(oModel)

     }

  })

Moreover, it means that the JSONModel in sapui5 is rather a JS object in essence?

Tula
Advisor
Advisor
0 Kudos

got it, thx:)

SergioG_TX
Active Contributor
0 Kudos

kindly makr your question answered/helpful

Tula
Advisor
Advisor
0 Kudos

Ok

Answers (1)

Answers (1)

Tula
Advisor
Advisor
0 Kudos

Just to insert the right answer... The right way to send the JSONModel data via ajax to server side is as follows:


var oModel = this.getView().getModel("model name");


$.ajax({

  url: // url of servlet

  type: "POST",

  contentType: "application/json;charset=utf-8",

  data: {

         jsonData: JSON.stringify(oModel.getData())

     }

  })

Don't forget to call getData() method in order to retrieve the JSON encoded data from the JSONModel:)

Regards,

La