Invalid JSON primitive: lookupID.“,”StackTrace“:”
我不明白为什么会出现这个错误。当我在变量周围加引号时,它会直接发送文本而不是变量。
{"Message":"Invalid JSON primitive: lookupID.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)
at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)
at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
我的Ajax函数:
1 2 3 4 5 6 7 8 9 | 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, ... |
它调用的方法:
1 2 | [WebMethod] public static string GetVisitDates(int LookupID, bool isMiscellaneous) |
在发送数据之前,首先需要
如果您使用浏览器的F12工具查看实际的
1 | LookupID=20&isMiscellaneous=true |
但是,为了正确绑定,它需要是实际的JSON,如下所示:
1 | {LookupID: 20, isMiscellaneous: true} |
这就是江户十一〔三〕进来的地方。
下面是我用来复制并确认这可以解决问题的代码:
在home.aspx.cs中:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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中的一个脚本):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | $(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放入它自己的变量中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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, ... |
或者,这可能有效,但在我的头上并不完全确定:
1 | data: { 'LookupID': lookupID, 'isMiscellaneous': IsMiscellaneous }, |
编辑:史蒂文·布莱恩是正确的答案。您要添加json.stringify()。我也更新了我的答案。
另请参见:无效的JSON原语错误