Tuesday, January 31, 2012

Traditional option in jquery ajax calls

I've started getting into Wcf Web Api, which is really nice tech. One issue I was having though is that in my ajax calls, that used json style data parameters, arrays were ending up being null on the .Net side (both mvc and my web api service). After some investigation I found that jquery used to have a different format for array inputs in their ajax call, which they later changed. It appears microsoft is still expecting that older style. The fix is simply to add traditional: true inside of your ajax calls. I've included some code below.

$.ajax({
                type: "POST",
                url: "your url",
                data: { input1: 10, input2: 1000, array1: [1, 2] },
                dataType: "json",
                traditional: true,
                success: function (data) {
                    // do something
                },
                error: function (data) {
                    alert("failed " + data);
                }
});

1 comment:

  1. to be specific, traditional:true is what makes the problem go away.

    ReplyDelete