cancel
Showing results for 
Search instead for 
Did you mean: 

JSON as default format

ParagJain
Participant
0 Kudos

Is it possible to specify that the default format from SAP Gateway is json and incase the "client" wants it in XML, etc. they can pass the $format parameter.

This is to avoid coding $format=json in all the clients (specially UI5 AJAX calls). We are sometimes lazy and forget to do this and thereby ending up with XML which is detected later.

Regards,

Parag.

Accepted Solutions (1)

Accepted Solutions (1)

AbhishekSharma
Active Contributor
0 Kudos

Hi Parag,

If you want response always in JSON format you can use JSONModel as below


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

See below example code

getOrderDetails: function(oEvent) {

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

  //Make model global by setModel so that it can be accessed in other controllers also.

  sap.ui.getCore().setModel(oLine_Model, "Order_Model");

  //Navigate to other View

  this.router.navTo("order_details");

  }

And in Component.JS file you can define your Service setting like below

config: {

  viewType: "JS",

  viewPath: "view",

  targetControl: "<your app name>", //Name of Split App given in App.view.js file

  clearTarget: false,

  transition: "slide",

  "serviceConfig": {

  "name": "<your service name created in Gateway>",

  "serviceUrl": "/sap/opu/odata/sap/<your service name created in Gateway>/"

  }

  },

Just to Understand how this works :

Another way to get response in JSON you can have AJAX call as below

jQuery.ajax({

    url: "/path/to/data.json",  // for different servers cross-domain restrictions need to be handled

    dataType: "json",

    success: function(data, textStatus, jqXHR) { // callback called when data is received

        oModel.setData({data: data});            // fill the received data into the JSONModel

    },

    error: function(jqXHR, textStatus, errorThrown) {

        alert("error occurred");

    }

});

You can put below line in Component.JS once and use the object name to setModel.

var oLine_Model = new sap.ui.model.json.JSONModel(); --> in Component.JS


sap.ui.getCore().setModel(oLine_Model, "Order_Model"); --> In your code

If you are running service dirlectly from SEGW then you have to provide $format=JSON in Query string then only you will get response in JSON format.

But in code you can use above suggested things to get response in JSON format always.

Hope this will help you.

Thanks-

Abhishek

ParagJain
Participant
0 Kudos

Thank you for the comprehensive answer and example.

We are primarily using AJAX, I am actually wanting to avoid this " dataType: "json", ", as programmers we tend to be lazy.

What would be interesting is if i dont provide this explicitly as json and bind it to a json model, will it still work or given an exception ?

AbhishekSharma
Active Contributor
0 Kudos

Hi Parag,

In most of the cases your code will not give any error because it all depends on the MIME type of Response which you are getting from JQuery call. These MIME type could be XML,JSON,JSONP,HTML etc.

If response MIME type is XML then you will get an XML as response and true for all others too..

At the time of making call if you are not sure of response type from that service (assuming you have written response handling logic for JSON only) then why not write

DATATYPE:JSON nothing harm in that just two words.

I would suggest if still you don't want to write go for any of method below:

  • var oLine_Model = new sap.ui.model.json.JSONModel();
  • Make a generic method which takes URL as an input parameter, the callback function name and returns you output in JSON.
  • Another option for you is getJSON method of JQuery

     <script src="jquery-1.7.1.js" type="text/javascript"></script>
     <script>
         $(document).ready(function () { 
             $("#Save").click(function () {
                 $.getJSON('<URL here>', function (data) {
                     console.log(data);
                 });

             });

         });
    </script>

The parameter "data" will contain your response from URL.

  • Try using below

myAjaxRequest: {

url: '<your url>'

data: {

     contenttype: 'application/json'

        }

}

If your requirement (just guessing) is call just a url cross domain then you need to use JSONP as data type.

Ref url: http://api.jquery.com/jQuery.ajax/

Hope this will help you.

Thanks-

Abhishek

Answers (1)

Answers (1)

AshwinDutt
Active Contributor
0 Kudos

Hello Parag,

As far as i know we always need to specify format needed in the URL if we want the GW response in JSON or XML.

When we do not specify $format=json, then GW always sends response in XML.

Regards,

Ashwin