我一直在尝试很多可能的解决方案,但都没有奏效,我有这样的东西,但我不知道 json 结构是否正确,或者我没有将它正确发送到服务器端,我想知道问题的根源在哪里。这是错误:没有为“System.String[]”类型定义无参数构造函数。
客户端
function upload() {
var title = [];
var files = $('#myfile').prop("files");
var names = $.map(files, function (val) { return val.name; });
for (var i = 0; i < names.length; ++i) {
var item = { 'titulo' : names[i] };
title.push(item);
}
var myJSON = JSON.stringify({ titulos: title });
console.log(myJSON);
$.ajax({
async: true,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: "POST",
data: myJSON,
url: "../handlers/saveUpload.ashx",
success: function (msg) {
$("#title").val("Sucesso");
$("#msg").val("Upload efectuado com sucesso.");
$("#alerta").show();
$("#gif").hide();
}
});
}
服务器端
try
{
date = DateTime.Now.ToShortDateString();
sessao = int.Parse(context.Session["userid"].ToString());
context.Response.ContentType = "application/json";
var data = context.Request;
var sr = new StreamReader(data.InputStream);
var stream = sr.ReadToEnd();
var javaScriptSerializer = new JavaScriptSerializer();
var arrayOfStrings = javaScriptSerializer.Deserialize<string[]>(stream);
foreach (var item in arrayOfStrings)
{
context.Response.Write(item.ToString());
}
}
catch (Exception msg) { context.Response.Write(msg.Message); }