JcSaint
JcSaint

Reputation: 115

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

Upvotes: 0

Views: 455

Answers (1)

Owuor
Owuor

Reputation: 616

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.

Upvotes: 1

Related Questions