0
votes

I want to use breeze with odata service: http://services.odata.org/V3/OData/OData.svc

    breeze.config.initializeAdapterInstances({dataService: "OData"});
    var ms = new breeze.MetadataStore();
    ms.fetchMetadata("http://services.odata.org/V3/OData/OData.svc")
    .then(function(rawMetadata) {...});

or, like this:

    breeze.config.initializeAdapterInstances({dataService: "OData"});
    var em = new breeze.EntityManager("http://services.odata.org/V3/OData/OData.svc");
    em.fetchMetadata()
    .then(function(rawMetadata) {...});

But, as I see breeze tries to fetch odata in json format when this service supports only atom/xml metadata.

Response header: Content-Type:application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8
Error message:
OPTIONS http://services.odata.org/V3/OData/OData.svc/$metadata. 501 Not Implemented
XMLHttpRequest cannot load http://services.odata.org/V3/OData/OData.svc/$metadata. Invalid HTTP status code 501

When I call this URL with jQuery, everything goes fine:

$.ajax({
     url: 'http://services.odata.org/V3/OData/OData.svc/$metadata',
     dataType: 'xml',
     type: 'GET'
}).done(function(data, st, xhr) {...});

and this scenario works too:

OData.defaultHttpClient.formatQueryString = '$format=application/json;odata=fullmetadata';
OData.defaultHttpClient.enableJsonpCallback = true;
OData.read("http://services.odata.org/V3/OData/OData.svc/Products",
        function (data, response) { alert(data.results[0]); }, 
        function (err) { alert(err.trace); }
);

but this scenario doesn't work:

OData.read("http://services.odata.org/V3/OData/OData.svc/$metadata",
        function (data, response) { alert(data.results[0]); }, 
        function (err) { alert(err.trace); },
        OData.metadataHandler);
1

1 Answers

0
votes

You are wrong with your assumption here that the above mentioned service only supports atom/xml format. You can very that by putting ?$format=jsonin the end of the URL.

Anyway to answer your question in case your real service doesn't actually support json you need to modify the header of requests that you make using breeze. Breeze in its stack uses Data.js to make oData calls. you can simply go ahead and put the below code before initializing your dataService.

var oldClient = odatajs.oData.defaultHttpClient;

        var myClient = {
            request: function (request, success, error) {
                //request.headers.Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
                var oHeaders = {
                    'Accept': 'text/html,application/xhtml+xml,application/xml,application/json;odata.metadata=minimal',
                    "Odata-Version": "4.0",
                    "OData-MaxVersion": "4.0",
                    "Prefer": "odata.allow-entityreferences"
                };
                request.headers = oHeaders;
                return oldClient.request(request, success, error);
            }
        };

        odatajs.oData.defaultHttpClient = myClient;