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

Advertisements
This entry was posted in .NET and tagged , . Bookmark the permalink.

17 Responses to jQuery Plugin for ASP.net Ajax (jMsAjax)

  1. Pingback: jQuery Plugin for ASP.net Ajax (jMsAjax) - Adam Schroder

  2. Pingback: jQuery, AJAX, ASP.NET and Dates | Schotime.net

  3. jim says:

    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. Pingback: Dataset, Datatable to Json | Schotime.net

  5. Paul says:

    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 says:

    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 says:

    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. bret says:

    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.

  9. Andi says:

    I have found a serious bug while using this plugin.
    When deserializing lot of data, problem occurs when jsonp callback function “slice” unicode char or decimal.
    This bug causes java script error in browser.

    I have described this problem in more details on asp.net forum: here

  10. Mainly wished to submit and question where you obtained your theme? I’m looking for one for my current site and really appreciate yours. Thanks.

  11. TimN says:

    Hi Schotime.

    Thanks for the effort in this plugin.
    I’ve had problems with it though and I really hope they are easily resolved…

    Note: I’m using jQuery 1.7.1

    I cant even get your example going.
    I’m using your web service method (in an asmx) and have modified it (very) slightly it to:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public DateTime getTime(DateTime date_in)
    {
    return date_in.AddDays(5);
    }

    It works fine if invoke it directly.

    When I use jmsajax I get an alert saying: “status: OK, message: undefined”

    And the success function doesnt execute.
    Firebug indicates that the actual POST has gone smoothly, so it must be something going wrong in the post processing…

    I also slightly modified your test call – but not significantly, I dont think:

    function jmsajax_test() {
    $.jmsajax({
    type: “POST”,
    url: “WebServiceTesting.asmx/getTime”,
    dataType: “msjson”,
    data: { date_in: new Date() },
    success: function (data) {
    //$(“#div”).html(String(data));
    var fred = data;
    fred = null;
    }
    });
    }

    I’m still digging around but I cant see why… any suggestions?

    Feel free to reply to the email provided.

    Nice party pics too, btw πŸ˜‰
    I see you like beer!

    Cheers,
    Tim.

  12. TimN says:

    Hi again… more details… I should have sent before…

    The actual posting of dates works.
    It’s only the processing out of dates received that breaks.

    A breakpoint in the example web service shows that the date gets through and the response contains the date data for an increment of 5 days…

    The error seems to be in the msJSON function.

    (which is still minified, btw).

    Cheers,
    Tim.

  13. TimN says:

    Heya Schotime…

    I still havent resolved this… been using a workaround but really do need to get it sorted if I’m going to use jmsajax (and I do want to).

    Could you please reply to my email address so I can send you an example JSON reply string that fails during post processing?

    I think then you’ll see what I cant… why it’s snagging up in msJSON?

    I tried to dig deeper myself but the msJSON function is minified and I cant figure out what’s doing in there πŸ˜‰

    Cheers,
    Tim.

  14. ck says:

    Hi, in order to get this plugin to even make the call (by way of $.ajax), at the end of the $.jmsajax function, just prior to the return $.ajax(options); call I placed:
    if (options.dataType && options.dataType == “msjson”)
    {
    options.dataType = “json”;
    }

    Otherwise the $.ajax(options) call fails.
    But it’s still failing in the success wrapper function. It appears that msJSON.parse is calling “replace” on a non-string object.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s