JcSaint JcSaint - 13 days ago 6
Ajax Question

Parameters lost zeros in Ajax request

I send parameters for

WebMethod
with
Ajax
and receive um object. But, paramters lost
zeros
in
WebMethod
, i send "00001234", in backend paramenters is "1234"

Ajax Code:

var content = {valueS: "00001234" };
$.ajax({
type: "GET",
dataType: 'json',
data: content,
async: false,
url: "myPage.aspx/GetData",
contentType: "application/json; charset=utf-8",
success: function (data) { OnSucess(data); },
error: function(data) { OnError(data); }
});


Webmethod Code:

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
public static string GetData(string valueS)
{...}

Answer

Try

 var content = {valueS: "'00001234'" };
 $.ajax({
            type: "GET",
            dataType: 'json',
            data: content,
            async: false,
            url: "myPage.aspx/GetData",
            contentType: "application/json; charset=utf-8",
            success: function (data) { OnSucess(data); },
            error:   function(data) { OnError(data); }
        });

seems valueS is being deserialized to a number first, in which case it will loose its leading zeros. The request generated by your code looks like this myPage.aspx/GetData?valueS=00001234

while the one generated by my code looks like this myPage.aspx/GetData?valueS=%2700001234%27, hence will be correctly deserialized as a string.

Comments