on 06-08-2016 2:38 PM
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
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> ),
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.