jQuery Plugin for ASP.net Ajax (jMsAjax)
After my recent post on jQuery, JSON and dates in asp.net I decided that there must be a better solution so I set out writing my first jQuery plugin. And after a few hours I had it working to my delight.
It not only accepts a raw JSON object as input for method parameters, but safely parses and stringify’s the object using Crockford’s implementation (http://www.json.org/js.html). The syntax of the call doesn’t change if it is a “POST” or a “GET” as it is all handled inside the plugin. It also returns Dates without a problem.
I have add a Home page for the plugin at http://schotime.net/jMsAjax.aspx and also added it to the plugins pages on the jQuery website (http://plugins.jquery.com/project/jMsAjax).
Here is the basic syntax:
$.jmsajax({
type: "POST",
url: "jMsAjax.aspx",
method: "getTime",
data: { date_in: new Date() },
success: function(data) {
$("#div").html(String(data));
}
});
|
You can find more information, a demo and the download at http://schotime.net/jMsAjax.aspx
Update: Just fixed the download page. Sorry for the inconvenience.
And at only 4kb + jQuery its significantly less size than MsAjax.
I hope this is as helpful to you as it is for me.
Schotime


[...] Click here to keep reading…. [...]
[...] jQuery Plugin for ASP.net Ajax (jMsAjax) [...]
Schotime,
Awesome for laying the groundwork in getting this resolved. I love the simplicity of exposing ASP.NET WebMethod’s and invoking them through the jQuery.ajax() functionality with the JSON dataType.
I thought utilizing the dataFilter functionality in the options for jQuery.ajax() proved to help solve the problem without writing a new plugin.
I still really like the incorporation of Crockford’s json.org/json.js for pre-parsing the JSON string prior to evaluation during the ajax() call – but is that too much?
I have a writeup for the simple dataFilter function declaration: http://www.overset.com/2008/07/18/simple-jquery-json-aspnet-webservice-datetime-support/
Thanks again for helping to resolve this issue! Great work!
[...] jQuery Plugin for ASP.net Ajax (jMsAjax) [...]
The date’s are incorrectly given to the Web Service as the web service is expecting them to be relative to UTC.
It was fixed by amending:
return ‘”\\/Date(‘ + (value.getTime()) + ‘)\\/”‘;
To:
return ‘”\\/Date(‘ + (Date.UTC(value.getFullYear(), value.getMonth(), value.getDate())) + ‘)\\/”‘;
It would also be nice if you provided a non-minified version of the source code.
Great plugin…
I noticed that in the example you are able to send a date and get a date back from the web service. It seems to be that the date you get back, is not a javascript date object but a string representation of the date. Is it possible to get an actual date back- or is there an easy way to convert this string representation of a date to a javascript date object.
The plugin doesn’t seem to handle dates with a negative value in the constructor. In our case, we had a datetime that was default constructed. In javascript, this has the value of Date(-62135578800000), but the regex didn’t pick up on it because of the negative sign, so it remained a string. We were then trying to perform date operations on string, and that’s when things blew up.
The problem lies in the regular expression that is being used which doesn’t take into account a negative sign. I added an optional negative sign in the regex:
a=/^\/Date\((-?[0-9]+)\)\/$/.exec(value);
instead of:
a=/^\/Date\(([0-9]+)\)\/$/.exec(value);
Also, would it be possible to publish a non-minified version of your code? You have probably one of the best plugins that we’ve found and fixing any problems is much harder when you don’t have the human-readable version.
Thanks Daniel.
I will update the source and post include an unminified version tomorrow.
Cheers Mate.
The new jQuery just rolled out 1.3.2 and I see that your plugin is for jQuery 1.0. I was using it with jQuery 1.2.6 and it worked fine but after upgrading I am getting errors in IE. Anyone else getting these errors.
What errors are you getting?