【问题标题】:Alert message is not displayed using Asp.net ajax使用 Asp.net ajax 不显示警报消息
【发布时间】:2019-05-23 14:19:47
【问题描述】:

使用Asp.net Ajax将记录添加到数据库后不显示警报消息。记录成功添加到数据库中。不显示警报。我在下面附上了我到目前为止尝试过的代码。添加记录后如何返回ajex成功函数以显示警报(“成功”);

表单设计

<form  id="frmProject" runat="server">
            <div>
                <label class="form-label">First Name</label>     
               <input type="text" id="fname" class="form-control"  />
            </div>
            <div class="form-group" align="left">
             <label class="form-label">Age</label>
                <input type="text" id="age" class="form-control"  />
            </div>
            <div> 
               <input type="button" id="b1" value="add" class="form-control" onclick="addProject()" />

            </div>
        </form>

Ajex

  function addProject() {
                $.ajax({
                    type: 'POST',
                    url: 'insert.aspx',
                    dataType: 'JSON',
                    data: {fname: $('#fname').val(), age: $('#age').val()},
                    success: function (data) {         
                        alert("success");
                        get_all();
                    },
                    error: function (xhr, status, error) {
                        console.log(xhr.responseText);
                    }
                });

                }

插入.aspx

 public static string GetData(string fname, int age)
            {
                SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;");
                string sql = "insert into record values('" + fname + "','" + age + "')";
                SqlCommand cmd = new SqlCommand(sql, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                return HttpContext.Current.Session.SessionID;
            }  

【问题讨论】:

  • 控制台是否输出任何错误?如果在success函数中放置一个console.log语句,它会记录吗?
  • 我修改了 insert.aspx 页面,这里的字符串 sql 出现错误
  • 控制台不显示错误

标签: asp.net json ajax webforms


【解决方案1】:

使用 Web 方法进行 Ajax 请求

[WebMethod(EnableSession= true)]
public static string GetData(string fname,int age)    
{    
   // place your logic here
   return HttpContext.Current.Session.SessionID;    
}

您的 Ajax 请求

$('#b1').click(function () {
    jQuery.ajax({
        url: 'insert.aspx/GetData',
        type: "POST",
        data: {fname: $('#fname').val(), age: $('#age').val()},
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            alert(JSON.stringify(data));
        }
    });
});

【讨论】:

  • public static string GetData(string fname, int age) { SqlConnection con = new SqlConnection("server=.; Initial Catalog = jds; Integrated Security= true;"); string sql = "插入记录值('" + fname + "','" + age + "')"; SqlCommand cmd = new SqlCommand(sql, con); con.Open(); cmd.ExecuteNonQuery(); con.Close();返回 HttpContext.Current.Session.SessionID;它不工作先生没有错误显示
  • [WebMethod(EnableSession= true)] 不工作我使用的是 Asp.net Simple forms not MVC
  • 你能帮我写代码吗先生,通俗易懂
  • 你的sql查询是否正常。如果您有更多列,则可能是在插入查询中也指定 cilumn。插入记录(col1,col2)值(val,val2)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 2014-02-21
  • 1970-01-01
相关资源
最近更新 更多