jQuery的阿贾克斯,从mvc.net控制器返回成功/错误 [英] Jquery Ajax, return success/error from mvc.net controller

查看:277
本文介绍了jQuery的阿贾克斯,从mvc.net控制器返回成功/错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想控制何时回复一条错误消息,当成功的消息,但我总是得到错误信息:

这里就是我要做的:

  $阿贾克斯({
                键入:POST,
                数据:FORMDATA,
                网址:/表格/ GetJobData
                数据类型:JSON,
                的contentType:假的,
                过程数据:假的,                成功:函数(响应){
                   警报(成功!)
                },
                错误:功能(响应){
                   警报(错误)//我总是得到这个。
                }            });

控制器:

  [HttpPost]
            公众的ActionResult GetJobData(乔布斯jobData)
            {              VAR mime类型= jobData.File.ContentType;
              VAR isFileSupported = AllowedMimeTypes(mime类型);             如果(!isFileSupported){
                     //错误
                    Response.Status code =(INT)的HTTPStatus code.BadRequest;
                    返回内容(以下简称附加文件,不支持,MediaTypeNames.Text.Plain);
             }
            其他
              {
                    //成功
                    Response.Status code =(INT)的HTTPStatus code.OK;
                    返回的内容(信息发送!,MediaTypeNames.Text.Plain);               }            }


解决方案

  $阿贾克斯({
                键入:POST,
                数据:FORMDATA,
                网址:/表格/ GetJobData
                数据类型:JSON,
                的contentType:假的,
                过程数据:假的,
                成功:函数(响应){
                    如果(响应= NULL&放大器;!&安培; response.success){
                        警报(response.responseText);
                    }其他{
                        // DoSomethingElse()
                        警报(response.responseText);
                    }
                },
                错误:功能(响应){
                    警报(错误!); //
                }            });

控制器:

  [HttpPost]
            公众的ActionResult GetJobData(乔布斯jobData)
            {              VAR mime类型= jobData.File.ContentType;
              VAR isFileSupported = AllowedMimeTypes(mime类型);             如果(!isFileSupported){
                     //错误
                    返回JSON(新{成功=假,responseText的=附加的文件不被支持。},JsonRequestBehavior.AllowGet);
             }
            其他
              {
                    //成功
                    返回JSON(新{成功= TRUE,responseText的=您的消息发送!},JsonRequestBehavior.AllowGet);
               }            }

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

               }   

            }

解决方案

 $.ajax({
                type: "POST",
                data: formData,
                url: "/Forms/GetJobData",
                dataType: 'json',
                contentType: false,
                processData: false,               
                success: function (response) {
                    if (response != null && 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 = AllowedMimeTypes(mimeType);

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

            }

这篇关于jQuery的阿贾克斯,从mvc.net控制器返回成功/错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆