更新时间:2023-02-25 17:33:26
$阿贾克斯({
键入:POST,
数据:FORMDATA,
网址:/表格/ GetJobData
数据类型:JSON,
的contentType:假的,
过程数据:假的,
成功:函数(响应){
如果(响应= NULL&放大器;!&安培; response.success){
警报(response.responseText);
}其他{
// DoSomethingElse()
警报(response.responseText);
}
},
错误:功能(响应){
警报(错误!); //
} });
控制器:
[HttpPost]
公众的ActionResult GetJobData(乔布斯jobData)
{ VAR mime类型= jobData.File.ContentType;
VAR isFileSupported = AllowedMimeTypes(mime类型); 如果(!isFileSupported){
//错误
返回JSON(新{成功=假,responseText的=附加的文件不被支持。},JsonRequestBehavior.AllowGet);
}
其他
{
//成功
返回JSON(新{成功= TRUE,responseText的=您的消息发送!},JsonRequestBehavior.AllowGet);
} }
I would like to control when to reply an error message and when a success message but I am always get the error message:
here is what I am trying to do:
$.ajax({
type: "POST",
data: formData,
url: "/Forms/GetJobData",
dataType: 'json',
contentType: false,
processData: false,
success: function (response) {
alert("success!")
},
error: function (response) {
alert("error") // I'm always get this.
}
});
Controller:
[HttpPost]
public ActionResult GetJobData(Jobs jobData)
{
var mimeType = jobData.File.ContentType;
var isFileSupported = AllowedMimeTypes(mimeType);
if (!isFileSupported){
// Error
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Content("The attached file is not supported", MediaTypeNames.Text.Plain);
}
else
{
// Success
Response.StatusCode = (int)HttpStatusCode.OK;
return Content("Message sent!", MediaTypeNames.Text.Plain);
}
}
$.ajax({
type: "POST",
data: formData,
url: "/Forms/GetJobData",
dataType: 'json',
contentType: false,
processData: false,
success: function (response) {
if (response != null && response.success) {
alert(response.responseText);
} else {
// DoSomethingElse()
alert(response.responseText);
}
},
error: function (response) {
alert("error!"); //
}
});
Controller:
[HttpPost]
public ActionResult GetJobData(Jobs jobData)
{
var mimeType = jobData.File.ContentType;
var isFileSupported = AllowedMimeTypes(mimeType);
if (!isFileSupported){
// Error
return Json(new { success = false, responseText = "The attached file is not supported." }, JsonRequestBehavior.AllowGet);
}
else
{
// Success
return Json(new { success = true, responseText= "Your message successfuly sent!"}, JsonRequestBehavior.AllowGet);
}
}