我正在尝试使用 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 - GET (JSON) 正在删除前导零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39990081/