我遇到了关于AJAX请求的问题,POST-s形成了控制器,但没有回复。没有成功也没有错误。我尝试了各种各样的组合,甚至留下了一些注释的代码也没有用,在某些情况下它会给出响应但是错误,比如如果我把超时放在ajax上(这是预期的)。我甚至尝试过简单的请求,没有表单提交,而是几个变量,但成功的反应从未到来。 有什么建议吗?
我的Jquery:
函数SubmitForm(){
var dataString;
if ($("#UpitId").attr("enctype") == "multipart/form-data") {
//this only works in some browsers.
//purpose? to submit files over ajax. because screw iframes.
//also, we need to call .get(0) on the jQuery element to turn it into a regular DOM element so that FormData can use it.
dataString = new FormData($("#UpitId").get(0));
contentType = false;
processData = false;
}
$.ajax({
url: "/Upit/Uspjeh/",
type: "POST",
dataType: "json",
//dataType: "html",
traditional: true,
data: dataString,
//data: { Sadrzaj:"Test Sadržaj",Eposta:"marko@gmail.com"},
contentType: contentType,
processData: processData,
//timeout:1,
sucess: function (data) {
alert("Sucess");
},
error: function (xhr) {
alert("Error");
}
});
}
控制器:
[HttpPost]
public ActionResult Uspjeh(FormaZaUnos model, string Send, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
string fileName = "";
byte[] fileText = null;
int numFiles = 0;
string IncidentId = _repository.CreateCrmRecord(model, fileName, fileText, numFiles);
if (Request.Files.Count > 0)
{
numFiles = Request.Files.Count;
for (int i = 0; i < numFiles; i++)
{
var upFile = Request.Files[i];
//var file = Request.Files[0];
if (upFile != null && upFile.ContentLength > 0)
{
var allowedExtensions = new[] { ".doc", ".xlsx", ".txt", ".jpeg", ".docx", ".xls" };
if (allowedExtensions.Contains(Path.GetExtension(upFile.FileName)))
{
_repository.CreateNotesWithAttachment(Request.Files[i], IncidentId);
}
}
}
}
//return Json("chamara", JsonRequestBehavior.AllowGet);
return Json(new { sucess=true }, JsonRequestBehavior.AllowGet);
//var storiesObj = new { success = true, stories = "Test" };
//return storiesObj;
//return Json("'Success':'true'");
//return Json(new { success = true, responseText = "Your message successfuly sent!" }, JsonRequestBehavior.AllowGet);
//return Content("uspjeh");
}
}
return null;
}