Aximili
Aximili

Reputation: 29464

GET (JSON) is removing leading zeros

I am trying to use jQuery to pass "0002" to a WebMethod. But the leading zeros are truncated :(

$.ajax({
  type: "GET",
  url: "CallNote.aspx/GetStoreRegion?storeCode=0002",
  contentType: "application/json; charset=utf-8",
  //dataType: "json", - Brad is right I don't need this line
  success: function (response) {
    console.log(response.d);
  }
});

In CallNote.aspx.cs:

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string GetStoreRegion(string storeCode)
{
  // Problem: Here storeCode becomes "2", not "0002"
  return myService.GetStoreRegion(storeCode);
}

How do you pass the string "0002" correctly?

Upvotes: 1

Views: 1656

Answers (1)

aquinas
aquinas

Reputation: 23786

So, you're telling ASP.NET that you'r passing the data as JSON. So, ASP.NET believes you. So, what would happen if you said var x = {storeCode: 0002};. Well, it would get turned into 2 because you aren't surrounding it in quotes. So you need to do the same thing for your parameter. If you want a string you'd do: var x = {storeCode: '0002'}; so in your case you want:

url: "CallNote.aspx/GetStoreRegion?storeCode='0002'",

Upvotes: 3

Related Questions