C# 在C中读取和解析POST请求中的JSON数据#
我正在通过JQuery的Ajax进行POST请求,数据类型定义为C# 在C中读取和解析POST请求中的JSON数据#,c#,jquery,ajax,json,post,C#,Jquery,Ajax,Json,Post,我正在通过JQuery的Ajax进行POST请求,数据类型定义为json,包含要发布到服务器的值,类似于用户名:“Ali” 在处理程序中,我需要做的是读取值,将它们反序列化为名为User的对象 String data = new System.IO.StreamReader(context.Request.InputStream).ReadToEnd(); User user = JsonConvert.DeserializeObject<User>(data); 从POST请求读
json
,包含要发布到服务器的值,类似于用户名:“Ali”
在处理程序中,我需要做的是读取值,将它们反序列化为名为User
的对象
String data = new System.IO.StreamReader(context.Request.InputStream).ReadToEnd();
User user = JsonConvert.DeserializeObject<User>(data);
从POST请求读取JSON
数据的正确方法是什么
客户端
$.ajax({
type: 'POST',
url: "http://localhost:38504/DeviceService.ashx",
dataType: 'json',
data: {
Username: 'Ali',
Age: 2,
Email: 'test'
},
success: function (data) {
},
error: function (error) {
}
});
在c#文件中使用此选项。。。会给你你想要的结果
string username=Request.Form["Username"].ToString();
其他人也是如此。。。
我希望这对你有帮助
另一个答案,或者您可以发送这样的数据
$.ajax({
url: '../Ajax/Ajax_MasterManagement_Girdle.aspx',
data: "Age=5&id=2"
type: 'POST',
success: function (data) {
}
});
在c中得到这样的答案#
我不知道为什么你的数据串被编码成url(看起来是这样的) 但这可能会解决问题(尽管我不确定)
String data=new System.IO.StreamReader(context.Request.InputStream.ReadToEnd();
字符串fixedData=HttpServerUtility.UrlDecode(数据);
User=JsonConvert.DeserializeObject(fixedData);
将对象转换为json字符串:
$.ajax({
type: 'POST',
url: "http://localhost:38504/DeviceService.ashx",
dataType: 'json',
data: JSON.stringify({
Username: 'Ali',
Age: 2,
Email: 'test'
}),
success: function (data) {
},
error: function (error) {
}
});
发布从该post请求返回的字符串。@AshokDamani这不是
数据的值吗
?这是一个完整的字符串,Username=Ali&Age=2&Email=test
,重复同样的事情…看起来你的字符串编码像url。也许如果您将其传递给url解码器,它可能会工作()@Luke我添加了jQuery部分。
string Age=Request.Form["Age"].ToString();
String data = new System.IO.StreamReader(context.Request.InputStream).ReadToEnd();
String fixedData = HttpServerUtility.UrlDecode(data);
User user = JsonConvert.DeserializeObject<User>(fixedData);
$.ajax({
type: 'POST',
url: "http://localhost:38504/DeviceService.ashx",
dataType: 'json',
data: JSON.stringify({
Username: 'Ali',
Age: 2,
Email: 'test'
}),
success: function (data) {
},
error: function (error) {
}
});