【问题标题】:Alert Success or Error message for jQuery POST methodjQuery POST 方法的警报成功或错误消息
【发布时间】:2014-04-22 19:03:37
【问题描述】:

我正在使用以下 jQuery 提交和保存数据:

var data = "Sample data";
$.post('@Url.Content("~/Student/SaveData")', { Data : data}
//I want to alert SUCCESS or ERROR here

这是我的控制器:

public ActionResult SaveData(string data)
{
  try
  {
      //Add logic to save data
      //pass SUCCESS indication.
  }
  catch(Exception ex)
  {
      //pass ERROR indication
  }
return View();
}

如果数据保存成功,我想提醒成功消息,否则错误消息。 jQuery部分如何处理?

【问题讨论】:

    标签: c# jquery asp.net-mvc


    【解决方案1】:

    您首先使用了错误的助手而不是 @Url.Content,您需要使用 @Url.Action 为您的操作生成 url:

    并像这样使用$.ajax

     var data = "Sample data";
    
    $.ajax({
                    url: '@Url.Action("SaveData","Student")',
                    type: "get",
                    data: { sample:data}
                    success: function (res) {
                        alert("success");
                    },
                    error: function () {
                        alert("failure");
    
                    }
                });
    

    你的行为会是这样的:

    public ActionResult SaveData(string sample="")
    {
    
    return Content(sample);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多