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

后端 未结 3 539
庸人自扰
庸人自扰 2020-12-07 23:52

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:

3条回答
  •  旧时难觅i
    2020-12-08 00:44

    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',
    

提交回复
热议问题