Main Contents

jQuery Plugin for ASP.net Ajax (jMsAjax)

Schotime @ July 1, 2008

.NET

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


book mark jQuery Plugin for ASP.net Ajax (jMsAjax) in del.icio.us submit jQuery Plugin for ASP.net Ajax (jMsAjax) to digg.com


10 Comments

  1. jQuery Plugin for ASP.net Ajax (jMsAjax) - Adam Schroder July 1, 2008 @ 11:29 pm

    [...] Click here to keep reading…. [...]

  2. jQuery, AJAX, ASP.NET and Dates | Schotime.net July 4, 2008 @ 8:30 am

    [...] jQuery Plugin for ASP.net Ajax (jMsAjax) [...]

  3. jim July 19, 2008 @ 11:54 am

    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!

  4. Dataset, Datatable to Json | Schotime.net July 27, 2008 @ 3:51 pm

    [...] jQuery Plugin for ASP.net Ajax (jMsAjax) [...]

  5. Paul October 1, 2008 @ 5:07 am

    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.

  6. eric December 6, 2008 @ 1:35 am

    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.

  7. Daniel February 11, 2009 @ 4:17 am

    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.

  8. Schotime February 12, 2009 @ 9:06 pm

    Thanks Daniel.
    I will update the source and post include an unminified version tomorrow.
    Cheers Mate.

  9. bret February 27, 2009 @ 9:30 am

    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.

  10. Schotime February 27, 2009 @ 10:10 am

    What errors are you getting?

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Feed
4,897 spam comments
blocked by
Akismet