无效的 JSON 原语:查找 ID.“,”堆栈跟踪“:”.
本文关键字:堆栈 跟踪 查找 JSON 原语 无效 ID | 更新日期: 2023-09-27 18:33:27
我不知道为什么会出现此错误。 当我在变量周围加上引号时,它实际上发送的是文本而不是变量。
{"Message":"Invalid JSON primitive: lookupID.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject((''r' at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth(''r' at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth(''r''n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth(''r' at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer(''r' at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit(''r' at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input(''r' at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContextcontext, JavaScriptSerializer serializer(''r' at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context(''r' at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData(","ExceptionType":"System.ArgumentException"}
我的 ajax 函数:
function GetVisitDates(lookupID, IsMiscellaneous) {
$.ajax({
type: "POST",
url: "Home.aspx/GetVisitDates",
data: "{ 'LookupID': lookupID, 'isMiscellaneous': IsMiscellaneous }",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
...
它调用的方法:
[WebMethod]
public static string GetVisitDates(int LookupID, bool isMiscellaneous)
发送数据之前,您需要先JSON.stringify()
数据 - 不幸的是.aspx
WebMethods对如何绑定参数非常挑剔:)
如果使用浏览器的 F12 工具查看实际的POST
请求,您将看到代码当前发送的有效负载如下所示:
LookupID=20&isMiscellaneous=true
但是,为了正确绑定,它必须是实际的 JSON,如下所示:
{LookupID: 20, isMiscellaneous: true}
这就是JSON.stringify
的用武之地。
这是我用来复制并确认这解决了问题的代码:
在主页.aspx.cs:
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetVisitDates(int LookupID, bool isMiscellaneous)
{
return string.Format("{0}-{1}", LookupID, isMiscellaneous);
}
}
在客户端(在我的例子中只是 Home.aspx 中的一个脚本(:
$(document).ready(function() {
GetVisitDates(20, true);
});
function GetVisitDates(lookupID, isMiscellaneous) {
var data = {
"LookupID": lookupID,
"isMiscellaneous": isMiscellaneous
};
data = JSON.stringify(data);
$.ajax({
type: "POST",
url: "Home.aspx/GetVisitDates",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function(data) {
console.log(data);
},
error: function(xhr) {
console.log(xhr);
}
}
);
}
最后,这里有一个参考,非常适合将ajax与Webforms一起使用...希望对您有所帮助!
引号会抛弃它。
我会先将 JSON 放入它自己的变量中:
function GetVisitDates(lookupID, IsMiscellaneous) {
var data = {
"LookupID" : lookupID,
"isMiscellaneous" : isMiscellaneous
};
$.ajax({
type: "POST",
url: "Home.aspx/GetVisitDates",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
...
或者,这可能会起作用,在我的头顶上并不完全确定:
data: { 'LookupID': lookupID, 'isMiscellaneous': IsMiscellaneous },
编辑:史蒂文布莱恩是正确的答案。你想要添加 JSON.stringify((。我也更新了我的答案。
另请参阅:无效的 JSON 基元错误