【发布时间】:2014-12-23 15:57:19
【问题描述】:
我想控制何时回复错误消息以及何时回复成功消息,但我总是收到错误消息:
这是我想要做的:
$.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.
}
});
控制器:
[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);
}
}
【问题讨论】:
-
添加
if条件...我不确定您在这里期待什么答案。 -
您的点击错误,因为第一个 return 语句之后的代码没有运行。您可能希望将代码移到成功注释之后,前一个 return 语句之前。
-
我解决了这个问题。现在我的问题很清楚了。
-
这个问题已经有好几年了,但是您可能收到错误的原因是您的 Ajax 请求中的
datatype参数。您告诉 Ajax 期待 JSON,但您返回的是纯文本:Ajax: "datatype": The type of data that you're expecting back from the server
标签: jquery asp.net-mvc