Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Ajax, return success/error from mvc.net controller

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);                      }                 } 
like image 956
Eyal Avatar asked Oct 28 '14 09:10

Eyal


2 Answers

 $.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!");  //      }  }); 

Controller:

[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:---

basically you can send multiple parameters this way:

Controller:

 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!");                  }             }); 
like image 113
Eyal Avatar answered Sep 28 '22 11:09

Eyal


Use Json class instead of Content as shown following:

    //  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);     } 

Also set contentType:

contentType: 'application/json; charset=utf-8', 
like image 27
SBirthare Avatar answered Sep 28 '22 09:09

SBirthare