JQuery Json error: Object does not support this property or method
ERROR: Microsoft JScript Runtime Error: Object does not support this property or method
this is how json2 is calling from:
**var json = JSON2.stringify(settings.data);**
I am using WCF service to pull data and it is very easy to test and it returns data to me from wcf service but it doesnt work on json2.js at line number 314-316
// We split the first stage into 4 regexp operations in order to work around
// crippling inefficiencies in IE and Safari regexp engines. First we
// replace all backslash pairs with '@' (a non-JSON character). Second, we
// replace all simple value tokens with ']' characters. Third, we delete all
// open brackets that follow a colon or comma or that begin the text. Finally,
// we look to see that the remaining characters are only whitespace or ']' or
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
if (/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
that's what I'm doing
// *** Generic Service Proxy class that can be used to
// *** call JSON Services generically using jQuery
// *** Depends on JSON2 modified for MS Ajax usage
function serviceProxy(wjOrderServiceURL) {
var _I = this;
this.ServiceURL = wjOrderServiceURL;
// *** Call a wrapped object
this.invoke = function (options) {
// Default settings
var settings = {
serviceMethod: '',
data: null,
callback: null,
error: null,
type: "POST",
processData: false,
contentType: "application/json",
dataType: "text",
bare: false
};
if (options) {
$.extend(settings, options);
}
// *** Convert input data into JSON - REQUIRES Json2.js
var json = JSON2.stringify(settings.data);
// *** The service endpoint URL
var url = _I.ServiceURL + settings.serviceMethod;
debugger
$.ajax({
url: url,
data: json,
type: settings.type,
processData: settings.processData,
contentType: settings.contentType,
timeout: settings.timeout,
error: settings.error,
dataType: settings.dataType,
success:
function (res) {
if (!settings.callback) { return; }
// *** Use json library so we can fix up MS AJAX dates
var result = JSON2.parse(res);
debugger
if (result.ExceptionDetail) {
OnPageError(result.Message);
return;
}
// *** Bare message IS result
if (settings.bare)
{ settings.callback(result); return; }
//http://encosia.com/2009/07/21/simplify-calling-asp-net-ajax-services-from-jquery/
if (result.hasOwnProperty('d'))
{ return settings.callback(result.d); }
else
{ return result; }
// *** Wrapped message contains top level object node
// *** strip it off
// for (var property in result) {
// settings.callback(result[property]);
// break;
// }
}
});
};
}
function GetFederalHolidays() {
$("#dContacts1").empty().html('Searching for Active Contacts...');
ContactServiceProxy.invoke({ serviceMethod: "Holidays",
callback: function (response) {
// ProcessActiveContacts1(response);
debugger
},
error: function (xhr, errorMsg, thrown) {
postErrorAndUnBlockUI(xhr, errorMsg, thrown);
}
});
}
wcf returns me a simple string
public List<string> Holidays()
{
List<string> s = new List<string>();
s.Add("01/01/2010");
s.Add("02/01/2010");
s.Add("03/01/2010");
s.Add("04/01/2010");
s.Add("05/01/2010");
return s;
}
any help what i am doing wrong? i am trying to go from dataType: text to json but i am getting the same error above.
a source to share
It looks like the bug is in Json2.js
. I am personally very skeptical when I see JSON libraries in JS. I would suggest trying something like this:
http://developer.yahoo.com/yui/json/
I've used it many times in the past and it was just great.
It should also be mentioned that the standard way to access JSON functions in a standards-compliant browser is something like JSON.stringify(...)
, not through JSON2
. If you're using a modern browser, you don't need to use a separate library for JSON.
Best wishes
a source to share