我想控制何时回复错误消息,何时回复成功消息,但我总是收到错误消息:

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.success) {
            alert(response.responseText);
        } else {
            // DoSomethingElse()
            alert(response.responseText);
        }                          
    },
    error: function (response) {
        alert("error!");  // 
    }

});

控制器:

[HttpPost]
public ActionResult GetJobData(Jobs jobData)
{
    var mimeType = jobData.File.ContentType;
    var isFileSupported = IsFileSupported(mimeType);

    if (!isFileSupported){        
         //  Send "false"
        return Json(new { success = false, responseText = "The attached file is not supported." }, JsonRequestBehavior.AllowGet);
    }
    else
    {
        //  Send "Success"
        return Json(new { success = true, responseText= "Your message successfuly sent!"}, JsonRequestBehavior.AllowGet);
    }   
}

---Supplement:---

基本上,您可以通过这种方式发送多个参数:

控制器:

 return Json(new { 
                success = true,
                Name = model.Name,
                Phone = model.Phone,
                Email = model.Email                                
            }, 
            JsonRequestBehavior.AllowGet);

Html:

<script> 
     $.ajax({
                type: "POST",
                url: '@Url.Action("GetData")',
                contentType: 'application/json; charset=utf-8',            
                success: function (response) {

                   if(response.success){ 
                      console.log(response.Name);
                      console.log(response.Phone);
                      console.log(response.Email);
                    }


                },
                error: function (response) {
                    alert("error!"); 
                }
            });

Jquery相关问答推荐

将日期时间从 javascript 传递给 c# (Controller)

如何确定 jQuery 中匹配元素的元素类型?

用按钮切换显示/隐藏div?

无法在被动事件侦听器中阻止默认值

JavaScript style.display="none" 还是 jQuery .hide() 更高效?

如何禁用在div内单击

如何使用 jQuery UI Resizable 仅水平或垂直调整大小?

jQuery计算所有文本字段中的值的总和

使用 时如何从 select2 中获取选定文本

jquery:更改URL地址而不重定向?

点击时保持 Bootstrap 下拉菜单打开

jQuery UI 中的 disableSelection 用于什么?

Bootstrap 折叠动画不流畅

延迟jquery悬停事件?

数据表日期排序dd/mm/yyyy问题

jQuery : eq() 与 get()

jQuery或javascript查找页面的内存使用情况

用于发布和获取的 Ajax 教程

jQuery 使用 AND 和 OR 运算符按属性 Select

AJAX 成功中的 $(this) 不起作用