Thursday, March 24, 2011

Can an asterisk in the text cause a getJSON request to not work?

This is really strange but this code is working fine unless the value entered by the user includes an asterik (*) or other characters like $ or #. The #ticketNumber.Val() is the suspected problem. Some of our id's have an * in them. Any help would be appreciated.

function buttonClicks() {
    var action = '/ServiceCall/IsAServiceCall/' + $('#ticketNumber').val() + '?x=' + new Date().getTime();
    $('#ticketNumberFilter').hide();
    $('#loading').show();
    $.getJSON(action,
        { ticketNumber: $("#ticketNumber").val() },
        function(callData) {
            if (callData.status == true) {
                window.location = "/ServiceCall/Show/" + $("#ticketNumber").val();
            }
            else {
                $('#loading').hide()
                $('#ticketNumberFilter').show();
                $("#ticketListMessage").slideDown("slow");
                $("#ticketNumber").val("");
            }
        });
}

Here's the controller: When there is an *, the controller never gets hit:

    public JsonResult IsAServiceCall(string ticketNumber)
    {
        IServiceCallService scService = new ServiceCallService();
        return (Json(new { status = scService.IsAServiceCall(ticketNumber) } ));
    }
From stackoverflow
  • A * shouldn't cause a problem but a # will - you should URL-encode the value using encodeURIComponent():

    var action = '/ServiceCall/IsAServiceCall/' + encodeURIComponent($('#ticketNumber').val()) + '?x=' + new Date().getTime();
    

    The same goes for your window.location

    Mike Roosa : Your answer helped with the # and other characters but the asterik is still causing an issue.
    Greg : If you got to the URL directly do you get the right response? I'd suspect a bug in the URL rewriting or your controller. The fact it's called ticket*Number* - could it be accidentally converted to an integer somewhere?
  • My guess is that (as RoBorg suggests), the URL rewriter that is used by the MVC framework considers * to be a special character... I can't be sure of what the problem is, but it seems like you could avoid the issue pretty easily by just removing asterisks before the request and adding them back again on the server-side:

    var ticketNumber = $('#ticketNumber').val().split('*').join('&asterisk;');
    

    and server-side you could reverse the process.

0 comments:

Post a Comment