Tina Chen
Tina Chen

Reputation: 2030

How to set Content-Type:application/json in ui5 oData.read() ?

When I call XSet/$count, I found the response is in xml, which is hard to parse.

I tried to call

oModel.read("/XSet/$count", {
    urlParameters: "$format=json",
    filters: [new Filter(this._oFilterState.aTaskFilter, false)],       
});

calledXSet/$count?$format=json&$filter=(status eq 'NOT_STARTED')

returned

"System query option '$format' is not compatible with the return type."

But XSet/$count?$filter=(status eq 'NOT_STARTED')&$format=json can return a json format error response.

I want to try the second way, which is change Content-Type: application/xml to Content-Type: application/json. But failed to find this in API: https://sapui5.hana.ondemand.com/#/api/sap.ui.model.odata.v2.ODataModel/methods/read

Upvotes: 0

Views: 3320

Answers (1)

Stephen S
Stephen S

Reputation: 3994

The Model.read method requests for an XML response by setting the Accept header as

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

However the count request is a plaintext response. You could get the count in two ways, one would be setting the Model's payload to use json and the other way would be a jQuery AJAX call.

You could initialize the model with a json parameter set to true.

var oModel = sap.ui.model.odata.v2.ODataModel("Service_URL",{
   json:true
});

This would pass a header with Accept:application/json

Upvotes: 1

Related Questions