Cant update lists in sharerpoint using datajas -- 'mediaType' is null or not an object

Nov 19, 2012 at 4:20 PM
Edited Nov 20, 2012 at 6:13 PM

I'm trying to use datajs 1.0.3 to update  sharepoint listitems. The save is working, but datajs always calls my error callback with returns an error message saying 'mediaType' is null or not an object

The code looks like this:
   var requestOptions = {
            requestUri: spListItem.__metadata.uri,
            method: "POST",
            data: createObjectFromSPListItem(spListItem),
            headers: {
                "If-Match": spListItem.__metadata.etag,
                "X-HTTP-Method": "MERGE"

            }
        };
        OData.request(requestOptions,
          function (xhr) {
              alert('saved');
          },
          function (xhr) {
              alert('save failed' + xhr.message + " " + xhr.response.statusText); //xhr message says 'mediaType' is null or not an object'
          }
          );


The request in fiddler looks like this :
POST http://mysitename.sharepoint.com/TeamSite/_vti_bin/listdata.svc/Caregories(2) HTTP/1.1
Content-Type: application/json;odata=verbose
Accept-Language: en-us
If-Match: W/"2"
dataserviceversion: 1.0
x-http-method: MERGE
Accept: application/json,application/json;odata=verbose
maxdataserviceversion: 2.0
Referer: http://mysitename.sharepoint.com/TeamSite/SitePages/Home.aspx
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB7.4; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MS-RTC LM 8; InfoPath.3; .NET4.0C; .NET4.0E)
Host: mysitename.sharepoint.com
Content-Length: 60
Connection: Keep-Alive
Pragma: no-cache
Cookie: FedAuth=xxxxxxxxA+; Ribbon.Library=1276846|0|8|-94849213; Ribbon.Read=1276846|-1|0|-94849213

{"Id":2,"Title":"cat2wdfa","Modified":"2012-11-19T07:52:49"}


and the response like this:
HTTP/1.1 204 No Content
Cache-Control: no-cache
ETag: W/"3"
Server: Microsoft-IIS/7.5
SPRequestGuid: ff1d9128-e48a-4e0f-ad8c-c5477177f50b
X-SharePointHealthScore: 0
DataServiceVersion: 1.0;
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 14.0.0.6120
P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
Date: Mon, 19 Nov 2012 15:54:57 GMT

 

I tried it on both my on prem site and on office 365 with the same error. How can I use datajs to update lists in Sharepoint without getting the error returned?

 

(

Nov 20, 2012 at 8:41 PM
Edited Nov 20, 2012 at 8:46 PM

So i traced though the datajs code a bit today to try to understand this  problem.

In the invokdeRequest function it calls handlerRead passing in the response. In ie8/9 the response has no headers at all because ie strips away headers when there is a 204 response. In chrome the headers from sharepoint are preserved, but there is infact no Content-Type header returned.


in handlerRead it gets the content type like this:

 var cType = getContentType(response);

ctype is now null since the response has no Content-Type header.

it then has a line that says

 if (handlerAccepts(handler, cType)) {


so it calls handlerAccepts with  a null ctype

inside of hanlderAccepts it says 
 

return handler.accept.indexOf(cType.mediaType) >= 0;

this throws an exception  because ctype is null. it has no mediaType property,

I fixed this in my environment by changing the code in handlerread (line 1283) from 
 

if (handlerAccepts(handler, cType)) {
...
}

to 
 

if (ctType != null && handlerAccepts(handler, cType)) {
...
}

This works in my invironment, but obviously, is houldnt be changing the library to suit my own environment.

has anyone used datajs to update existing items in a sharepoint list successfully?  Do i need to write a custom handler to read the sharepoint response.