我如何使用一起jquery,ajax和mysql

本文关键字:ajax mysql jquery 何使用 一起 | 更新日期: 2023-09-27 18:02:58

我有一些代码。我想添加jquery和ajax我的代码。我试过了,但我做不到。

这里是我的jQuery代码
$(document).ready(function () {
    $('#btnGOlustur_Click').click(function () {
        var Pro_id = $('#drop_p').val(); //drop_p is combobox' name.
        var Ity_id = $('#drop_i').val();
        var Sta_id = $('#drop_s').val();
        document.write("basliyorrrr");
        $.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            url: 'GorevEkle.aspx/btnGOlustur_Click',
            data: "{'project_id':'" + Pro_id + "','status_id':'" + Sta_id + "','itype_id':'" + Ity_id + "'}",
            async: false,
            success: function (response) {
                $('#drop_p').val(''); $('#drop_i').val(''); $('#drop_s').val('');
                alert("Record saved successfully in database");
                document.write("dataabase e kaydedildi");
            },
            error: function () {
                alert("some problem in saving data");
            }
        });
    });
});

My save cs file

[WebMethod]
[ScriptMethod]
protected void btnGOlustur_Click(object sender, EventArgs e)
{
    try
    {
        MySqlConnection con = new MySqlConnection(Globals.CONNECTION_STRING);
        con.Open();
        String UserId = Session["user_id"].ToString();
        int Pro_id = Convert.ToInt32(drop_p.SelectedValue);
        int Sta_id = Convert.ToInt32(drop_s.SelectedValue);
        int Ity_id = Convert.ToInt32(drop_i.SelectedValue);
        MySqlCommand cmd = new MySqlCommand("INSERT INTO issue (user_id, project_id, status_id, itype_id) values ('" + UserId + "','" + Pro_id + "','" + Sta_id + "','" + Ity_id + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
    catch (Exception err)
    {
        lbl_hata.Text = "Error reading list of names. ";
        lbl_hata.Text += err.Message;
    }
    Response.Redirect("Profil.aspx");
}

我的错误:

类型为"System.Threading"的第一次偶然异常。ThreadAbortException'在mscorlib.dll中发生
类型为System.Threading的异常。ThreadAbortException'在mscorlib.dll中发生,但未在用户代码中处理

我如何使用一起jquery,ajax和mysql

你的代码中有几个错误

  1. [WebMethod]应该是静态的。
  2. 将btnGOlustur_Click逻辑移动到单独的静态方法
  3. 你不能回复。ajax上下文中的重定向
  4. 从请求中钩取值。参数

考虑以上所有因素

[WebMethod]
[ScriptMethod]
public static string MyMethod()
{
    try
    {
        MySqlConnection con = new MySqlConnection(Globals.CONNECTION_STRING);
        con.Open();
        String UserId = HttpContext.Current.Session["user_id"].ToString();
        int Pro_id = HttpContext.Current.Request.Params["project_id"];
        //other values
        MySqlCommand cmd = new MySqlCommand("INSERT INTO issue (user_id, project_id, status_id, itype_id) values ('" + UserId + "','" + Pro_id + "','" + Sta_id + "','" + Ity_id + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
    catch (Exception err)
    {
        return "error";
    }
    return "success";
}
然后你的jQuery
$(document).ready(function () {
    $('#btnGOlustur_Click').click(function (e) {
        var Pro_id = $('#drop_p').val(); //drop_p is combobox' name.
        var Ity_id = $('#drop_i').val();
        var Sta_id = $('#drop_s').val();

        $.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            url: 'GorevEkle.aspx/MyMethod',
            data: "{'project_id':'" + Pro_id + "','status_id':'" + Sta_id + "','itype_id':'" + Ity_id + "'}",
            async: false,
            success: function (response) {
                if(response=="success"){
                $('#drop_p').val(''); $('#drop_i').val(''); $('#drop_s').val('');
                alert("Record saved successfully in database");
                }
                else{
                 //handle error
                }
            },
            error: function () {
                alert("some problem in saving data");
            }
        });
    });
});