开发者

JQuery ajax call does not take alphanumeric parameters!

开发者 https://www.devze.com 2022-12-09 04:04 出处:网络
JQuery seems to be giving an error when trying to pass an alphanumeric parameter like so: $.ajax({ type: \"POST\",

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 numeri开发者_JS百科c 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


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.)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消