invalidjson(invalidjsontokenline1ch1)
本篇文章给大家谈谈invalidjson,以及invalidjsontokenline1ch1对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、打开网页显示无效的json基元是什么意思
- 2、gee出现invalidjson
- 3、无效的json,如图。
- 4、我在用datatables ,提示Invalid JSON response.是怎么回事?
打开网页显示无效的json基元是什么意思
Dovov编程网
Dovov编程网 jQuery webmethod Ajax处理中的“JSON基元无效”
AJAX处理中的“JSON基元无效”
我得到一个来自jQuery的ajax调用错误。
这是我的jQuery函数:
function DeleteItem(RecordId, UId, XmlName, ItemType, UserProfileId) { var obj = { RecordId: RecordId, UserId: UId, UserProfileId: UserProfileId, ItemType: ItemType, FileName: XmlName }; var json = Sys.Serialization.JavaScriptSerializer.serialize(obj); $.ajax({ type: "POST", url: "冲磨EditUserProfile.aspx/DeleteRecord", data: json, contentType: "application/json; charset=utf-8", dataType: "json", async: true, cache: false, success: function(msg) { if (msg.d != null) { RefreshData(ItemType, msg.d); } }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("error occured during deleting"); } }); }
这是我的WebMethod :
[WebMethod] public static string DeleteRecord(Int64 RecordId, Int64 UserId, Int64 UserProfileId, string ItemType, string FileName) { try { string FilePath = HttpContext.Current.Server.MapPath(FileName); XDocument xmldoc = XDocument.Load(FilePath); XElement Xelm = xmldoc.Element("UserProfile"); XElement parentElement = Xelm.XPathSelectElement(ItemType + "/Fields"判判做); (from BO in parentElement.Descendants("Record") where BO.Element("Id").Attribute("value").Value == RecordId.ToString() select BO).Remove(); XDocument xdoc = XDocument.Parse(Xelm.ToString(), LoadOptions.PreserveWhitespace); xdoc.Save(FilePath); UserInfoHandler obj = new UserInfoHandler(); return obj.GetHTML(UserId, UserProfileId, FileName, ItemType, RecordId, Xelm).ToString(); } catch (Exception ex) { HandleException.LogError(ex, "EditUserProfile.aspx", "DeleteRecord"); } return "success"; }
任何人都可以告诉我,我的代掘衡码有什么问题吗?
我得到这个错误:
{ "Message":"Invalid JSON primitive: RecordId.", "StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() 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" }
只是一个猜测什么variablesjson包含之后
var json = Sys.Serialization.JavaScriptSerializer.serialize(obj);?
如果它是一个有效的json对象,如{'foo':'foovalue', 'bar':'barvalue'}那么jQuery可能不会将其作为json数据发送,而是将其序列化为foor=foovaluebar=barvalue "Invalid JSON primitive: foo"
尝试改为将数据设置为string
$.ajax({ ... data: "{'foo':'foovalue', 'bar':'barvalue'}", //note the additional quotation marks ... })
这样,jQuery应该单独保留数据,并将string按原样发送到服务器,以便ASP.NET能够parsingjson服务器端。
运用
data : JSON.stringify(obj)
在上面的情况下,我相信会有效果。
注意:你应该添加json2.js库所有的浏览器都不支持那个JSON对象(IE7-) json.js和json2.js之间的区别
如抖动所示, $.ajax函数将用作data参数的任何对象/数组序列化为url编码格式。 奇怪的是, dataType参数只适用于来自服务器的响应,而不适用于请求中的任何数据。
遇到同样的问题后,我下载并使用jquery-json插件将请求数据正确编码到ScriptService。 然后,使用$.toJSON函数来编码想要传送给服务器的参数:
$.ajax({ type: "POST", url: "EditUserProfile.aspx/DeleteRecord", data: $.toJSON(obj), contentType: "application/json; charset=utf-8", dataType: "json" .... });
jQuery的Ajax将默认发送数据作为查询string参数的forms如:
RecordId=456UserId=123
除非processData选项设置为false,在这种情况下,它将作为对象发送到服务器。
contentType选项是用于客户端发送数据格式的服务器。
dataType选项是用于服务器,它告诉什么types的数据客户端期待从服务器回来。
不要指定contentType,以便服务器将它们parsing为查询string参数而不是json。
要么
使用contentType作为'application / json; charset = utf-8'并使用JSON.stringify(object),以便服务器能够从string反序列化json。
它正在这样的工作
data: JSON.stringify({'id':x}),
我猜@Jitter是对的,但他的解决scheme对我来说并不合适。
这是它的工作原理:
$.ajax({ ... data: "{ intFoo: " + intFoo + " }", ... });
我没有尝试,但我认为如果参数是一个string,它应该是这样的:
$.ajax({ ... data: "{ intFoo: " + intFoo + ", strBar: '" + strBar + "' }", ... });
如果手动格式化JSON,这里有一个非常方便的validation器: jsonlint.com
使用双引号而不是单引号:
无效:
{ 'project': 'a2ab6ef4-1a8c-40cd-b561-2112b6baffd6', 'franchise': '110bcca5-cc74-416a-9e2a-f90a8c5f63a0' }
有效:
{ "project": "a2ab6ef4-1a8c-40cd-b561-2112b6baffd6", "franchise": "18e899f6-dd71-41b7-8c45-5dc0919679ef" }
我面临同样的问题,我带来了很好的解决scheme如下:
尝试这个…
$.ajax({ type: "POST", url: "EditUserProfile.aspx/DeleteRecord", data: '{RecordId: ' + RecordId + ', UserId: ' + UId + ', UserProfileId:' + UserProfileId + ', ItemType: \'' + ItemType + '\', FileName: '\' + XmlName + '\'}', contentType: "application/json; charset=utf-8", dataType: "json", async: true, cache: false, success: function(msg) { if (msg.d != null) { RefreshData(ItemType, msg.d); } }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("error occured during deleting"); } });
请注意这里stringtypes参数我已经使用(\)转义序列字符表示为string值。
在服务器上,序列化/反序列化json到自定义对象:
public static string SerializeT(T obj) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); MemoryStream ms = new MemoryStream(); serializer.WriteObject(ms, obj); string retVal = Encoding.UTF8.GetString(ms.ToArray()); return retVal; } public static T DeserializeT(string json) { T obj = Activator.CreateInstanceT(); MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)); DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); obj = (T)serializer.ReadObject(ms); ms.Close(); return obj; }
这些答案只是让我在无效参数和缺失参数之间来回跳动。
这工作对我来说,只是用引号包装stringvariables…
data: { RecordId: RecordId, UserId: UId, UserProfileId: UserProfileId, ItemType: '"' + ItemType + '"', FileName: '"' + XmlName + '"' }
CSS使一个空单元格的边框出现?将可变parameter passing给另一个接受可变参数列表的函数
jQuery优化/最佳实践
调整textarea事件?
向PrimeFaces添加jQuery会导致Uncaught TypeError覆盖整个地方
TypeError:$(…).modal不是具有引导模式的函数
jQuery:附加()对象,删除()它与延迟()
Javascript如何从string中删除文本
inputtypes=“文件”的jQuery更改方法
如何在使用jQuery Unobtrusivevalidation时添加“submitHandler”函数?
如何从jQuery中的当前元素开始select下一个“n”元素?
Search for...
Go!
Interesting Posts
如何使用免费的jqgrid属性有条件地添加操作button
我怎样才能添加,删除或交换页面的jQueryvalidation规则?
Google图表重绘/缩放窗口大小
jQuery的iframefile upload
如何select一个关注jQuery的元素
jQuery延期 – 等待多个AJAX请求完成
如何使用jQuery启用或禁用锚点?
JQuery中“param序列化的传统风格”是什么?
禁止Twitter Bootstrap模式窗口closures
焦点在IE中不起作用
Back to topDovov编程网
Copyright © Dovov 编程网 - All Rights Reserved.
gee出现invalidjson
gee出现invalidjson是语法有侍前错误。nvalidJSON错误导致的json对象不能解析,一般都是服务器返回的json字符串的语法有错误,这种情况下,只需要仔细的检塌斗查一下团谈磨json即可。
无效的json,如图。
/title @p title {"text":"Hello World"}
java版用升穗空搏这个格式,你那个是基岩版吵亏卜的输入格式
[img]我在用datatables ,提示Invalid JSON response.是怎么回事?
这是因为jquery1.6版销扒亩本对json要求严格了,导入
script src="js/jquery.json.min.js"/script
script src="js/jquery.json.js"此扒/亏森script
关于invalidjson和invalidjsontokenline1ch1的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。