Ajax发布会截断前导零 [英] ajax post truncating leading zero's

查看:76
本文介绍了Ajax发布会截断前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

$.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);
    }
});

InputTextBox中的值具有前导0,但是当将其发布到url时,前导0将被截断.

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

推荐答案

将json发送到服务器时,应该使用内置的JSON.stringify方法创建json,而不是手动创建.

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);
    }
});

这将导致发送"0005"而不是0005,后者在解析后将转换回字符串,而不是转换为将丢失前导零的数字.

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.

这篇关于Ajax发布会截断前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆