2014-10-28 65 views
41

我想控制返回成功/错误时回复一条错误消息,当成功的消息,但我总是得到错误信息: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);  

       } 

      } 
+0

添加'if'条件......我不知道是什么回答你在这里期待。 – 2014-10-28 09:45:04

+0

由于第一个返回语句之后的代码没有运行,因此您的命中错误。您可能希望在前面的返回语句之前,在成功注释之后移动代码。 – Collins 2014-10-28 09:49:11

+0

我修复了这个问题。现在我的问题很清楚。 – Eyal 2014-10-28 09:50:47

回答

75
$.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 = AllowedMimeTypes(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); 
    } 
} 

---补充:---

基本上就可以发送多个参数是这样的:

控制器:

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!"); 
       } 
      }); 
+1

'contentType:'application/json; charset = utf-8''或'false'?为什么? – 2015-06-22 00:18:04

+0

很好的解决方案谢谢 – 2016-05-21 17:11:54

+0

me added contentType:'application/json; charset = utf-8', processData:false,它为我工作。 – 2016-10-19 10:53:46

15

使用Json类,而不是Content如图以下:

// When I want to return an error: 
    if (!isFileSupported) 
    { 
     Response.StatusCode = (int) HttpStatusCode.BadRequest; 
     return Json("The attached file is not supported", MediaTypeNames.Text.Plain); 
    } 
    else 
    { 
     // When I want to return sucess: 
     Response.StatusCode = (int)HttpStatusCode.OK; 
     return Json("Message sent!", MediaTypeNames.Text.Plain); 
    } 

另外设置的contentType:

contentType: 'application/json; charset=utf-8', 
+0

'contentType:'application/json; charset = utf-8''或'false'?为什么? – 2015-06-22 00:17:51

+2

我喜欢这个提供“Response.StatusCode”集合的例子。 – 2016-09-08 14:51:29

+0

@ClintEastwood contentType是你发送的数据的类型,所以它需要设置为json,一个非常常见的是'application/json;字符集= UTF-8' 。 – 2017-03-24 16:57:06