Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following code:

$.ajax({
    type: "Post",
    url: 'http://example.com/jambo',
    contentType: "application/json",
    data: String(' ' + $('InputTextBox').val()),
    success: function (data) {
        alert("success");
    },
    error: function (msg) {
        var errmsg = JSON.stringify(msg);
        alert("message: " + errmsg);
    }
});

The value in InputTextBox has leading 0's but when this is posted to the url the leading 0's are truncated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
146 views
Welcome To Ask or Share your Answers For Others

1 Answer

When sending json to a server, you should use the built-in JSON.stringify method to create json rather than creating it manually.

$.ajax({
    type: "Post",
    url: 'http://example.com/jambo',
    contentType: "application/json",
    data: JSON.stringify($('InputTextBox').val()),
    success: function (data) {
        alert("success");
    },
    error: function (msg) {
        var errmsg = JSON.stringify(msg);
        alert("message: " + errmsg);
    }
});

This will result in sending "0005" instead of 0005, which when parsed, will be converted back into the string rather than a number which will lose the leading zeros.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...