1

When I use an AJAX request that sends data to controller with IE, it is sending null data. When I use Chrome or Firefox, my data from the key/value pairs are there in the controller's parameter variables.

Here is my javascript function:

    $(".input-group.date").datepicker({
        autoclose: true
    }).on('changeDate', function (ev) {


        var schoolId = $(this).attr('data-schoolId');
        var date = new Date(ev.date).toUTCString();
        var schoolName = $(this).attr('data-schoolName');

        $.ajax({
            url: "@Url.Action("ChangeFulfillmentDate", "Admin")",
            type: "POST",
            data: {
                'newDate': date,
                'schoolId': schoolId
            },
            success: function (data) {
                if (!data.success) {
                    var n = noty({
                        text: 'Hold up! Something went wrong...<br />' + data.message,
                        layout: 'top',
                        type: 'error',
                        killer: true,
                        closeWith: ['button']
                    });
                }
                else {
                    var n = noty({
                        text: 'Fulfillment date for ' + schoolName + ' updated successfully',
                        layout: 'bottomRight',
                        type: 'success',
                        timeout: 3000,
                        killer: true,
                        closeWith: ['hover']
                    });
                }
            },
            error: function (xhr, status, error) {
                var n = noty({
                    text: 'Hold up! Something went wrong...<br />' + xhr.responseText,
                    layout: 'top',
                    type: 'error',
                    killer: true,
                    closeWith: ['button']
                });
            }
        });
    });

The ajax response error I get is:

The parameters dictionary contains a null entry for parameter 'newDate' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.JsonResult ChangeFulfillmentDate(System.DateTime, Int32)' in 'CurrReplenishment.Controllers.AdminController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

Parameter name: parameters

Why is my AJAX call not sending the proper values for the key/value pairs? I have made sure that there is data in those javascript variables, as it works in Chrome and Firefox.

2
  • debug this line in IE. var date = new Date(ev.date).toUTCString(); your problem likely has nothing to do with ajax. Most likely IE doesn't recognize ev.date as a date string. Commented Dec 20, 2013 at 16:49
  • I don't think that's it. The 'date' variable gets populated with data that looks like this "Tue, 17 Jun 2014 00:00:00 UTC". Commented Dec 20, 2013 at 17:09

1 Answer 1

1

As it was previously commented, your problem lies with the date. JSon is very weird about dates and will not allow you to send it as you regularly send data. In order to make it work, you need to do something like this:

var mydate= dateFormat(yourdate, "mm/dd/yyyy HH:MM:ss");

You can read more about it here (bit old, but hey, it works)

Sign up to request clarification or add additional context in comments.

2 Comments

This works. Thanks for your help. I really appreciate the link to an explanation as to why this is happening rather than just telling me how to fix it.
It wasn't the most intuitive error. I've seen AJAX send null data if you construct the Data key/value pair wrong before, and that's what I thought it was doing since my 'date' had data in it. I would have never guessed the data in the 'date' variable was invalid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.