GET (JSON) 正在删除前导零
GET (JSON) is removing leading zeros
我正在尝试使用 jQuery 将“0002”传递给 WebMethod。但是前导零被截断了:(
$.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);
}
});
在CallNote.aspx.cs中:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string GetStoreRegion(string storeCode)
{
// Problem: Here storeCode becomes "2", not "0002"
return myService.GetStoreRegion(storeCode);
}
如何正确传递字符串“0002”?
因此,您告诉 ASP.NET 您正在以 JSON 的形式传递数据。所以,ASP.NET 相信你。那么,如果你说 var x = {storeCode: 0002};
会发生什么。好吧,它会变成 2
因为你没有用引号括起来。所以你需要为你的参数做同样的事情。如果你想要一个字符串,你会这样做: var x = {storeCode: '0002'};
所以在你的情况下你想要:
url: "CallNote.aspx/GetStoreRegion?storeCode='0002'",
我正在尝试使用 jQuery 将“0002”传递给 WebMethod。但是前导零被截断了:(
$.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);
}
});
在CallNote.aspx.cs中:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string GetStoreRegion(string storeCode)
{
// Problem: Here storeCode becomes "2", not "0002"
return myService.GetStoreRegion(storeCode);
}
如何正确传递字符串“0002”?
因此,您告诉 ASP.NET 您正在以 JSON 的形式传递数据。所以,ASP.NET 相信你。那么,如果你说 var x = {storeCode: 0002};
会发生什么。好吧,它会变成 2
因为你没有用引号括起来。所以你需要为你的参数做同样的事情。如果你想要一个字符串,你会这样做: var x = {storeCode: '0002'};
所以在你的情况下你想要:
url: "CallNote.aspx/GetStoreRegion?storeCode='0002'",