使用Ajax post时为Null参数
我已经找到了几个答案(例如),但它们似乎并不能解决我的问题使用Ajax post时为Null参数,ajax,asp.net-mvc-3,Ajax,Asp.net Mvc 3,我已经找到了几个答案(例如),但它们似乎并不能解决我的问题 var result = { command: 'exportWAV', type: type }; $.ajax({ url: 'SubmitSound', type: 'Post'
var result = {
command: 'exportWAV',
type: type
};
$.ajax({
url: 'SubmitSound',
type: 'Post',
data: JSON.stringify(result),
contentType: 'application/json; charset=utf-8',
success: function (msg) {
alert(msg);
}
});
后端代码
[HttpPost]
public ActionResult SubmitSound(string blob)
{
// Create the new, empty data file.
string fileName = AppDomain.CurrentDomain.BaseDirectory + "/Content/Sound/" + Environment.TickCount + ".wav";
FileStream fs = new FileStream(fileName, FileMode.CreateNew);
BinaryWriter w = new BinaryWriter(fs);
w.Write(blob);
w.Close();
fs.Close();
return new JsonResult() { Data = "Saved successfully" };
}
result
不为空,因为this.postMessage=result代码>将文件发送回客户端进行下载w.Write(blob)
不断抱怨blob
不能为null
我怎样才能让它工作?谢谢您,并向您致以最诚挚的问候请执行以下操作:
data: JSON.stringify({ blob: result}),
对于与JSON结构相同的对象,您可能需要在控制器操作中更改字符串参数。。。这意味着相同的属性名称
对象应该是这样的:
public class MyBlob{
public string command {get; set;}
public string type {get; set;}
}
因此,你的行动应该是:
public ActionResult SubmitSound(MyBlob blob){
//Here your action logic
}
我看不出像blob=result
现在command
和type
都是空的,我想我还没有深入挖掘它。谢谢你的想法