Jquery 为什么Ajax调用会在将数据发布到服务器之前自动执行错误和完成块
我有下面的代码,它在将表单数据发送到服务器之前序列化表单数据。然而,由于某些原因,这段代码在进行ajax调用之前首先执行error和complete块,尽管服务器返回纯文本,但从未执行success块Jquery 为什么Ajax调用会在将数据发布到服务器之前自动执行错误和完成块,jquery,ajax,asp.net-mvc,devexpress,Jquery,Ajax,Asp.net Mvc,Devexpress,我有下面的代码,它在将表单数据发送到服务器之前序列化表单数据。然而,由于某些原因,这段代码在进行ajax调用之前首先执行error和complete块,尽管服务器返回纯文本,但从未执行success块 $("#btnUpload").click(function () { debugger; UploadControl.Upload(); $.ajax({ type: 'POST', dataType: 'text', as
$("#btnUpload").click(function () {
debugger;
UploadControl.Upload();
$.ajax({
type: 'POST',
dataType: 'text',
async: false,
cache: false,
contentType: 'application/json; charset=utf-8',
url: $("#hdnUploadPOFile").val(),
data: $('#uploadControlForm').serialize(),
success: function (response) {
debugger;
// ProcessFileMessage(response);
toastr.success('File processed successfully');
},
error: function () {
alert('error');
},
complete: function () {
alert('hi');
}
})
});
下面是为控制器的操作编写的代码
[HttpPost]
public ContentResult UploadControlUpload(PrintempsFileUploadViewModel model)
{
string[] errors;
UploadedFile[] files = UploadControlExtension.GetUploadedFiles("UploadControl", UploadControlHelper.Settings, out errors, (s, e) => { }, UploadControl_FilesUploadComplete);
PrintempsFileUploadViewModel printempsFileUploadViewModel = Session["PrintempsFileUploadViewModel"] as PrintempsFileUploadViewModel;
string fileProcessingMessage = "Exceptions found"; // PrintempsFileHomeService.ProcessPrintempsPurchaseOrderData(model.Season, FileName);
return Content(fileProcessingMessage);
}
有人能帮我找出ajax代码的问题吗?请告诉我发送表单数据所需的最少代码是否有帮助: 视图:
@{
布局=空;
}
Index800
$(函数(){
$(“#btn”)。单击(功能(ev){
$.ajax({
类型:“POST”,
url:“/Home/Index800”,
数据:{message:$(“form”).serialize()},
数据类型:“json”,
成功:功能(响应){
$(“#输出”).val(JSON.stringify(response.AjaxDataSend));
},
错误:函数(结果){
警报(“错误”);
}
});
})
})
@*需要在ajaxing到服务器之前序列化名称attrib*@
控制器/型号I返回专有数据,以说明如何返回数据:
public class AjaxDataSend
{
public string from { get; set; }
public string to { get; set; }
}
public class HomeController : Controller
{
[HttpPost] //message needs to have same name as in .ajax
public ActionResult Index800(string message)
{
AjaxDataSend ads = new AjaxDataSend
{
from = "2017-05-03",
to = "2017-05-05"
};
AjaxDataSend ads2 = new AjaxDataSend
{
from = "2017-05-10",
to = "2017-05-12"
};
IList<AjaxDataSend> list = new List<AjaxDataSend>();
list.Add(ads);
list.Add(ads2);
return Json(new
{
AjaxDataSend = list
}
, @"application/json");
}
公共类AjaxDataSend
{
来自{get;set;}的公共字符串
{get;set;}的公共字符串
}
公共类HomeController:控制器
{
[HttpPost]//消息需要与.ajax中的名称相同
public ActionResult Index800(字符串消息)
{
AjaxDataSend ads=新的AjaxDataSend
{
from=“2017-05-03”,
至=“2017-05-05”
};
AjaxDataSend ads2=新的AjaxDataSend
{
from=“2017-05-10”,
至=“2017-05-12”
};
IList list=新列表();
列表。添加(ads);
增加(ads2);
返回Json(新的
{
AjaxDataSend=list
}
,@“应用程序/json”);
}
error callback可以获取参数…尝试使用它们查看错误并获得更好的帮助为什么需要此文本数据类型而不是“Json”:数据类型:“Text”,url:$(“#hdnUploadPOFile”).val(),是您的url$(“#hdnUploadPOFile”).val()是我的URL。我已尝试将数据类型更改为JSON,但没有任何帮助。@user1737473在您对将form.serialize()发送到服务器发表评论后,当我找到解决问题所需的确切更改时,我将在下面缩短我的答案。
public class AjaxDataSend
{
public string from { get; set; }
public string to { get; set; }
}
public class HomeController : Controller
{
[HttpPost] //message needs to have same name as in .ajax
public ActionResult Index800(string message)
{
AjaxDataSend ads = new AjaxDataSend
{
from = "2017-05-03",
to = "2017-05-05"
};
AjaxDataSend ads2 = new AjaxDataSend
{
from = "2017-05-10",
to = "2017-05-12"
};
IList<AjaxDataSend> list = new List<AjaxDataSend>();
list.Add(ads);
list.Add(ads2);
return Json(new
{
AjaxDataSend = list
}
, @"application/json");
}