I'm trying to send a custom object and an integer to my controller action using jQuery AJAX.
我正在尝试使用jQuery AJAX向控制器操作发送自定义对象和整数。
[HttpPost]
public JsonResult GetFilenameSuggestion(Document document, int tradeId)
{
//do stuff...
}
In my JS, I've tried to create the json sent to the controller in several ways, including:
在我的JS中,我尝试以多种方式创建发送到控制器的json,包括:
var json = JSON.stringify({ document: document, tradeId: tradeId });
//or
var json = { document: JSON.stringify(document), tradeId: tradeId };
//or
var json = { document: document, tradeId: tradeId };
and here's my jQuery AJAX:
这是我的jQuery AJAX:
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "/Document/GetFilenameSuggestion",
data: json,
error: function (XMLHttpRequest, textStatus, errorThrown) {
//do bad stuff...
},
success: function (data) {
//do good stuff...
});
Any suggestions on how to do this? I am getting an internal server error when the ajax posts, and I'm 99% sure it's due to how the arguments are being passed to the controller action.
有关如何做到这一点的任何建议?当ajax发布时,我收到内部服务器错误,我99%肯定这是由于参数如何传递给控制器操作。
1 个解决方案
#1
0
Use JSON.stringify
to convert your javascript object to it's json string version and specify the contentType
as "application/json
".
使用JSON.stringify将您的javascript对象转换为它的json字符串版本,并将contentType指定为“application / json”。
The below code should work fine.
以下代码应该可以正常工作。
var model = { document: { DocumentName: "Dummy" }, tradeId: 34 };
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "/Document/GetFilenameSuggestion",
data: JSON.stringify(model),
error: function(XMLHttpRequest, textStatus, errorThrown) {
//do bad stuff...
},
success: function(data) {
//do good stuff...
}
});
for this action method signature
对于此操作方法签名
[HttpPost]
public JsonResult GetFilenameSuggestion(Document document, int tradeId)
{
// to do : return some useful JSON data
}
#1
0
Use JSON.stringify
to convert your javascript object to it's json string version and specify the contentType
as "application/json
".
使用JSON.stringify将您的javascript对象转换为它的json字符串版本,并将contentType指定为“application / json”。
The below code should work fine.
以下代码应该可以正常工作。
var model = { document: { DocumentName: "Dummy" }, tradeId: 34 };
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "/Document/GetFilenameSuggestion",
data: JSON.stringify(model),
error: function(XMLHttpRequest, textStatus, errorThrown) {
//do bad stuff...
},
success: function(data) {
//do good stuff...
}
});
for this action method signature
对于此操作方法签名
[HttpPost]
public JsonResult GetFilenameSuggestion(Document document, int tradeId)
{
// to do : return some useful JSON data
}