Skip to Content

how to send json model via ajax?

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    Posted on Jun 08, 2016 at 01:40 PM

    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> ),

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 28, 2016 at 01:05 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.