views:

121

answers:

2

Hello all,

JQuery seems to be giving an error when trying to pass an alphanumeric parameter like so:

            $.ajax({
                type: "POST",
                url: "Default.aspx/AjaxTest",
                data: "{eventID:9a5}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    alert(msg.d);
                },
                error: function(e) {
                    alert("Event could not be added to calendar");
                }
            });

when the above method is called, the error callback is called. However when I change the eventID parameter to a purely numeric value, it works fine and the success callback is called. I would like to pass an alphanumeric value to a server method and this doesnt seem to work. Any help would be appreciated.

Ahmed

+2  A: 

Shouldn't you be passing data like a regular Javascript dictionary?

$.ajax({
...
data: {"eventID": "9a5", "SomeNumericField": 25}
...
});

(E.g: Don't put quotes around your data. I'm pretty sure it's not supposed to be a string like that.)

Bartek
Additionally check that the URL you're posting the data to will accept a non-numerical value for that parameter. It might be returning an error.
Steve Kemp
@Bartek: When I tried that, it didn't work for both numeric and alphanumeric, however it was working fine for numeric values before.@Steve: I have an asp.net webmethod which takes a string argument. Shouldn't that suffice?
Ahmed Refaat
A: 

I just learned how to resolve this problem. Turns out I was getting a JSON error message: "Invalid JSON primitive". I had to add additional single quotes around my string parameter so JSON would understand it was a string when it was deserializing it. I added single quotes around my alphanumeric data so that JSON would understand it was a string. This is how my code ended up working:

        $.ajax({
            type: "POST",
            url: "Default.aspx/AjaxTest",
            data: "{eventID:'9a5'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                alert(msg.d);
            },
            error: function(e) {
                alert("Event could not be added to calendar");
            }
        });

Thanks all anyway.

Ahmed Refaat