How to do an AJAX
call to a MVC
Controller
that expects a dynamic
object
?
如何对期望动态对象的MVC控制器执行AJAX调用?
This was my try:
这是我的尝试:
public JsonResult Test(dynamic someObject)
{
return;
}
$.ajax({
type: 'GET',
url: '/home/test',
data: JSON.stringify( { test: 1, test2: 2 } ),
contentType: 'application/json',
success: function (data) {
alert(data);
},
error: function (error, data) {
alert("something went wrong: " + error );
}
});
But I get just {object} as a value in someObject
.
但我在someObject中只得到{object}作为值。
1 个解决方案
#1
2
This isn't possible because MVC cannot deserialize an object that it doesn't know a type for.
这是不可能的,因为MVC不能反序列化它不知道类型的对象。
I recommend passing the data in as a JSON string and then deserializing that.
我建议将数据作为JSON字符串传入,然后反序列化。
public JsonResult Test(string someObject)
{
dynamic y = new JavaScriptSerializer().Deserialize<dynamic>(someObject);
return;
}
JavaScriptSerializer can be found in System.Web.Extensions
可以在System.Web.Extensions中找到JavaScriptSerializer
#1
2
This isn't possible because MVC cannot deserialize an object that it doesn't know a type for.
这是不可能的,因为MVC不能反序列化它不知道类型的对象。
I recommend passing the data in as a JSON string and then deserializing that.
我建议将数据作为JSON字符串传入,然后反序列化。
public JsonResult Test(string someObject)
{
dynamic y = new JavaScriptSerializer().Deserialize<dynamic>(someObject);
return;
}
JavaScriptSerializer can be found in System.Web.Extensions
可以在System.Web.Extensions中找到JavaScriptSerializer