Rad*_*dhi 92 c# jquery webmethod
我在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");
}
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的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";
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我我的代码有什么问题吗?
我收到此错误:
{
"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"
}
Run Code Online (Sandbox Code Playgroud)
jit*_*ter 128
只是猜测变量json包含什么
var json = Sys.Serialization.JavaScriptSerializer.serialize(obj);?
Run Code Online (Sandbox Code Playgroud)
如果它是一个有效的json对象,{'foo':'foovalue', 'bar':'barvalue'}那么jQuery可能不会将其作为json数据发送,而是序列化它,foor=foovalue&bar=barvalue从而得到错误"Invalid JSON primitive: foo"
尝试将数据设置为字符串
$.ajax({
...
data: "{'foo':'foovalue', 'bar':'barvalue'}", //note the additional quotation marks
...
})
Run Code Online (Sandbox Code Playgroud)
这样jQuery应该保留数据并将字符串原样发送到服务器,这应该允许ASP.NET解析json服务器端.
And*_*rew 100
运用
data : JSON.stringify(obj)
Run Code Online (Sandbox Code Playgroud)
在上述情况下我会相信.
注意:您应该添加json2.js库,所有浏览器都不支持该JSON对象(IE7-) json.js和json2.js之间的区别
lee*_*ers 18
如抖动所述,该$.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"
....
});
Run Code Online (Sandbox Code Playgroud)
小智 17
它的工作是这样的
data: JSON.stringify({'id':x}),
Run Code Online (Sandbox Code Playgroud)
T G*_*pta 12
Jquery Ajax将默认发送数据作为查询字符串参数形式如:
RecordId=456&UserId=123
Run Code Online (Sandbox Code Playgroud)
除非该processData选项设置为false,否则它将作为对象发送到服务器.
contentType option用于客户端以何种格式发送数据的服务器.
dataType option用于服务器,它告诉服务器期望从哪种类型的数据客户端返回.
不要指定contentType,以便服务器将它们解析为查询字符串参数而不是json.
要么
使用contentType作为'application/json; charset = utf-8'并使用JSON.stringify(object),以便服务器能够从字符串反序列化json.
我猜@jitter正确,但是他的解决方案对我不起作用。
这是它的工作原理:
$.ajax({
...
data: "{ intFoo: " + intFoo + " }",
...
});
Run Code Online (Sandbox Code Playgroud)
我没有尝试过,但我认为如果参数是字符串,则应该像这样:
$.ajax({
...
data: "{ intFoo: " + intFoo + ", strBar: '" + strBar + "' }",
...
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
230014 次 |
| 最近记录: |