我想控制返回成功/錯誤時回覆一條錯誤消息,當成功的消息,但我總是得到錯誤信息:jQuery的阿賈克斯,從mvc.net控制器
這裏就是我努力做到:
$.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'條件......我不知道是什麼回答你在這裏期待。 – 2014-10-28 09:45:04
由於第一個返回語句之後的代碼沒有運行,因此您的命中錯誤。您可能希望在前面的返回語句之前,在成功註釋之後移動代碼。 – Collins 2014-10-28 09:49:11
我修復了這個問題。現在我的問題很清楚。 – Eyal 2014-10-28 09:50:47