通过ajaxpost请求接收json对象
Receive json object through ajax post request
UNABLE TO DETECT PROBLEM
我正在尝试使用 ajax post 请求从服务器获取数据,但是当 ajax 请求到达后端 c# 方法时,其数据部分变为空
这是我的js代码
let da = '{"sidx":'+sid+',"idx":'+cur+'}';
da = JSON.parse(da);
$.ajax({
type: "POST",
url: "../RegSaleman/next",
data: {x:da},
datatype: "Json",
complete: function (dataret) {
}
});
c# 代码是
[HttpPost]
public JsonResult next(JsonResult x)
{
}
您正在尝试读取 JsonResult,这是错误的。此 class 用于来自服务器的 响应 。
您可以创建一些将由 MVC 框架自动映射的数据模型(简单 class)。
假设您有 JSON 个对象:
{
"id": "someValue",
"foo" : 3,
"bar" : "bar string"
}
你可以创建一个 class
public class MyClass
{
public string Id {get;set;}
public int Foo {get;set;}
public string Bar {get;set;}
}
如您所见,它甚至可以映射不同大小写的变量(如 Foo 和 "foo"),但这种行为可以根据需要进行更改。
您的方法将是:
[HttpPost]
public JsonResult next(MyClass x)
{
}
var obj = new Object();
$.ajax({
type: "POST",
url: "/RegSaleman/next",
data: JSON.stringify(o),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
},
failure: function (response) {
},
error: function (response) {
}
});
UNABLE TO DETECT PROBLEM
我正在尝试使用 ajax post 请求从服务器获取数据,但是当 ajax 请求到达后端 c# 方法时,其数据部分变为空
这是我的js代码
let da = '{"sidx":'+sid+',"idx":'+cur+'}';
da = JSON.parse(da);
$.ajax({
type: "POST",
url: "../RegSaleman/next",
data: {x:da},
datatype: "Json",
complete: function (dataret) {
}
});
c# 代码是
[HttpPost]
public JsonResult next(JsonResult x)
{
}
您正在尝试读取 JsonResult,这是错误的。此 class 用于来自服务器的 响应 。 您可以创建一些将由 MVC 框架自动映射的数据模型(简单 class)。 假设您有 JSON 个对象:
{
"id": "someValue",
"foo" : 3,
"bar" : "bar string"
}
你可以创建一个 class
public class MyClass
{
public string Id {get;set;}
public int Foo {get;set;}
public string Bar {get;set;}
}
如您所见,它甚至可以映射不同大小写的变量(如 Foo 和 "foo"),但这种行为可以根据需要进行更改。 您的方法将是:
[HttpPost]
public JsonResult next(MyClass x)
{
}
var obj = new Object();
$.ajax({
type: "POST",
url: "/RegSaleman/next",
data: JSON.stringify(o),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
},
failure: function (response) {
},
error: function (response) {
}
});