如何将带有消息的json对象返回给ajax函数 [英] How to return json object with message to ajax function

查看:102
本文介绍了如何将带有消息的json对象返回给ajax函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用相同的代码行返回带有自定义错误/成功消息的json对象:我有这两行代码:



I want to be able to return json object with a custom error/success message using the same line of code: i have these two lines of code:

return Json(data);
    return Json(new { f = "error" });





我希望能够像这样在一行显示:





I want to be able display it in one line like this:

return Json(data, Json(new { f = "error" }));





我将感谢任何人的帮助



我尝试过:



我的服务器端代码:





I will appreciate any assistance from anyone

What I have tried:

My Server side Code:

if (getId > 0)
      {
     var getList = appointmentRepository.GetAppointmentList(userId);
     var data = Newtonsoft.Json.JsonConvert.SerializeObject(getList);                       
     return Json(data);
     return Json(new { s = "success" });
     }
     else
      {
      var getList = appointmentRepository.GetAppointmentList(userId);
      var data = Newtonsoft.Json.JsonConvert.SerializeObject(getList);
      return Json(data);
      return Json(new { f = "error" });
     }







我的Ajax功能:






My Ajax Function:

<script type="text/javascript">

            $.ajax({
                url: '/Appointment/InsertPatientAppointment/',
                type: "POST",
                data: JSON.stringify({ appointmentDate: $(".datetimepicker").val() }),

                cache: false,
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (_data) {
                    if (_data.f !== undefined) {
                        swal({
                            title: "Failed!",
                            text: _data.f,
                            type: "info"
                        });
                        table.clear().draw();
                        return false;
                    }
                    else {
                        swal({
                            title: "Success!",
                            text: _data.s,
                            type: "success"
                        });

                    }
                }
            });
        });
    });

</script>

推荐答案

.ajax({
url:'/ Appointment / InsertPatientAppointment /',
类型:POST,
数据:JSON.stringify({appointmentDate:
.ajax({ url: '/Appointment/InsertPatientAppointment/', type: "POST", data: JSON.stringify({ appointmentDate:


(。datetimepicker)。val()} ),

cache:false,
dataType:json,
contentType:application / json; charset = utf-8,
success:function( _data){
if(_data.f!== undefined){
swal({
title:Failed!,
text:_data.f,
输入:info
});
table.clear()。draw();
返回false;
}
else {
swal( {
标题:成功!,
文本:_data.s,
类型:成功
});

}
}
});
});
});

< / script>
(".datetimepicker").val() }), cache: false, dataType: "json", contentType: "application/json; charset=utf-8", success: function (_data) { if (_data.f !== undefined) { swal({ title: "Failed!", text: _data.f, type: "info" }); table.clear().draw(); return false; } else { swal({ title: "Success!", text: _data.s, type: "success" }); } } }); }); }); </script>


您的代码中不能有多个return语句。如果要使用消息返回数据,则必须创建一个自定义对象,该对象组合并包装数据和自定义消息并返回该对象。例如,你可以创建一个这样的自定义对象:



You can't have multiple return statements in your code. If you want to return the data with message, then you have to create a custom object that combines and wrap both your data and custom message and return that object instead. For example you could create a custom object like this:

public class MyCustomResponse
{
    public object Data { get; set; }
    public string Message { get; set; }
}





然后在您的Controller中,您可以像这样返回该对象:





Then in your Controller, you can return that object like this:

var data = Newtonsoft.Json.JsonConvert.SerializeObject(getList); 
var response = new MyCustomResponse(){
	Data = data,
	Message = "Successful"
};

return Json(response);





您也可以阅读本文:

ASP.NET核心和Web API:用于管理异常和一致响应的自定义包装器 [ ^ ]

这篇关于如何将带有消息的json对象返回给ajax函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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