Monday, December 20, 2010

JSON, jQuery, IE, OData and a Parsererror

Quite a collection of technologies and approaches in that combination being used on a single page in SharePoint - mostly worked fine until I had deployed to a site using IE7. Suddenly my jQuery calls to $.getJSON() would silently fail, and with no error callback on the getJSON method, I resorted to an $.ajax call instead.

Experimenting in the error callback, found that even though the response status was success, there was an error - the error text was "parsererror". Seems that IE7's native parsing of JSON does behave too well for jQuery 1.4.2. Finally wrote some code in the error callback to detect when the err was parsererror, and in that instance to create a new JSON object using eval, similar to this:

$.ajax({
type: 'GET',
url: serviceUrl,
contentType: 'application/json: charset=utf-8',
dataType: "json",
success: ProcessResults,
error: function (xhr, textStatus, errThrown) {
if (textStatus == 'parsererror') {
var json;
try { json = eval('(' + xhr.responseText + ')'); }
catch (e) { }
ProcessResults(json);
}
}
});

After working out this fix, I found some information on stackoverflow suggesting that IE has problems with leading new line characters at the start of the JSON in the HTTP response body.

Thursday, December 9, 2010

SharePoint Designer and generate-id

Just wanted to note down a great tip from Marc D Anderson (who derived the idea from a post by Jorge Vasquez) on how to work around the annoyance of SharePoint Designer 2007 inserting generate-id() statements into the ID attributes of HTML tags in customised XSL in SharePoint 2007 or WSS3 pages.

The small steps to applying this fix are as follows:
1. Create an empty dummy variable in the XSL , for example
<xsl:variable name="DummyVar">
2. Append this dummy parameter to any IDs, e.g.
<input id="myid{$DummyVar}" type=......