suraj jain
suraj jain

Reputation: 1032

jQuery.ajax stripping out spaces

    <html>
<head>
<title>Untitled 1</title>
<script language="javascript" type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
    $("#usermsg").keypress(function(e) {
        if (e.keyCode == 13) {
            var message = $("#usermsg").val();
            alert("sent :" + message);
            $.ajax({
                type: "POST",
                url: "post1.asp?postchat=1",
                data: "text=" + message,
                success: function(data) {
                    alert("rcvd :" + data);
                }
            });
           $("#usermsg").attr("value", '');

        }
    });
});</script>
</head>

<body>

<form method="post">
    <textarea id="usermsg" name="TextArea1"></textarea></form>

</body>

</html>

I've run into a problem that I haven't seen before - on a very simple $.ajax{} powered form using POST, when the string pairs are passed to my processing page, somewhere along the line any spaces are being stripped out of the values. An alert(dataString); before the $.ajax{} call shows that the spaces exist, but when it's submitted to the processing page the strings have no spaces in them. Do I have to urlencode/decode the strings somewhere, or is something else messing things up? Anyone come across this before?

Upvotes: 0

Views: 1153

Answers (2)

user1136564
user1136564

Reputation: 1

The easy answer is to use data: 'get' instead of data: 'post'

Upvotes: 0

kim3er
kim3er

Reputation: 6476

Have you tried the following line in your ajax call?

data: {text: message},

jQuery should be taking care of the encoding for you.

Rich

Upvotes: 2

Related Questions