1

Resolved

"no handler for data" when reading text/html media link

description

After switching from 0.0.3 to 0.0.4 I'm getting error "no handler for data" when reading media link of content type text/html:
 
OData.read({
            requestUri: myUrl + "/Document('docid')/$value",
            headers: {
                'Accept': 'text/html'
            }
        }, function (data, response) {
        }, function (err) {
        });

comments

ATrigo wrote Jun 24, 2011 at 8:34 PM

Can you please share with us a capture of the http response? Also, are you using JSONP?

epodolyak wrote Jun 24, 2011 at 9:05 PM

No, I'm using Cross-domain Ajax.

Http response:

HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Type: text/html
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
Access-Control-Allow-Origin: http://owl.localhost.com
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Allow-Headers: Authorization, Accept
X-ServerName: PodolyakPC
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
Set-Cookie: ASP.NET_SessionId=1ivi3dyl00ces2n1jcu3ag53; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Fri, 24 Jun 2011 20:01:56 GMT
Content-Length: 4918

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
....

ATrigo wrote Jun 30, 2011 at 1:45 AM

Thank you for the capture. The thing is that the content you are receiving is text/html. The library doesn´t have a text/html handler out of the box (which is intended); however, the library is extensible in this regards and you can very easily inject your own handler. You can do it on a per request basis or replacing the default OData handler.

Per request basis:
var textHtmlHandler = { 
    read: function(response, context){
        var contentType = response.headers["Content-Type"];
        if( contentType && contentType.indexOf("text/html") === 0){
            response.data = response.body;
        } else {
            odata.defaultHandler.read(response, context); 
        }
    },
    write: function(request, context){}
}

OData.read(myUrl + "/Document('docid')/$value",
    function success (data, response) {}, 
    function error (err) {},
    textHtmlHandler);
Or replacing the OData.defaultHandler:
var oldDefaultHandler = OData.defaultHandler;   
OData.defaultHandler = { 
accept: oldDefaultHandler.accept,
    read: function(response, context){
        var contentType = response.headers["Content-Type"];
        if( contentType && contentType.indexOf("text/html") === 0){
            response.data = response.body;
        } else {
            oldDefaultHandler.read(response, context); 
        }
    },
    write: function(request, context){
        oldDefaultHandler.write(request, context);
    }
};

OData.read(myUrl + "/Document('docid')/$value",
    function success (data, response) {}, 
    function error (err) {});

epodolyak wrote Jun 30, 2011 at 10:41 AM

Thanks you, I will try this solution next week and will comment there with the results, but please consider to add a generic handler which will simply return "response" object for content types without specific handler defined.