Pit Digger
Pit Digger

Reputation: 9800

Jquery AJAX with WebMethod removes zero from front

I have following code. Jquery Ajax calls webmethod . If i pass zipcode "07306" it returns and sets session to "7306" . No idea why it removes zero from front!

function  onChangeLocation(){
        var newzip =$('#<%= txtNewLocation.ClientID %>').val();
        $('#<%= lblDefaultLocation.ClientID %>').html(newzip);
            $.ajax({
          type: "POST",
          url: "/WebMethods.aspx/ChangeLocation",
          data: "{newLocation:" + newzip + "}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(msg) {
          alert(msg.d);
          }
            });
       }


     [System.Web.Services.WebMethod()]
            public static String ChangeLocation(String newLocation)
            {

                HttpContext.Current.Session["ClientZipCode"] = newLocation.ToString();
                return newLocation.ToString();
            }

Can someone please explain why it removes zero from front ?

Upvotes: 3

Views: 825

Answers (1)

Korich
Korich

Reputation: 1284

The problem is that JS thinks it an integer changing

$('#<%= lblDefaultLocation.ClientID %>').html(newzip);

to

$('#<%= lblDefaultLocation.ClientID %>').html(newzip + '');

Should fix it.

Upvotes: 1

Related Questions