1

I am trying to call a ASP.NET WebAPI method and pass it a serialized JavaScript object over a GET request similar to a $.ajax POST request. It is working fine except for when the JavaScript object has a null value for Networks, then it sends it in the URL like /api/airings/get-airings?networks= and the server is showing the string[] as having a single item with a value of null! If I remove the network= it shows up as null on the server as I would expect.

So, it appears that people requested tickets in jQuery regarding this behavior, but I want to know what I can do without using a plugin or rewriting the $.param logic or messing with WebAPI ModelBinder.

http://bugs.jquery.com/ticket/8653

http://bugs.jquery.com/ticket/1814

public class ApiGetAiringsRequest
{
    public DateTime? DateStart { get; set; }
    public DateTime? DateFinish { get; set; }
    public string[] Networks { get; set; }
}

public HttpResponseMessage GetAirings([FromUri] ApiGetAiringsRequest request )
    ...

document.location.href = "/api/airings/get-airings?" + $.param(request);
2

1 Answer 1

0

so to work around this issue I changed the string array to a simple string, and do a .split on it.

var serialize = function(obj)
{
    var str = [];

    for (var p in obj)
    {
        if (obj[p])
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
    }

    return str.join("&");
};

$.get('/api/airings/get-airings?' + serialize(request));

C#

var networks = (request.Networks??"").Split(',');

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.