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

时间:2014-05-13 09:38:54

标签: c# jquery mysql ajax

我有一些代码。我想在我的代码中添加jquery和ajaxin。我试过但我没做过。

这是我的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");
            }
        });
    });
});

我的保存cs文件

[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中但未在用户代码中处理

1 个答案:

答案 0 :(得分:0)

您的代码中存在几个错误

  1. [WebMethod]应该是静态的来调用它。
  2. 将btnGOlustur_Click逻辑移至单独的静态方法
  3. 您无法在ajax上下文中制作Response.Redirect
  4. 将值从Request.Params
  5. 挂钩

    考虑以上所有

    [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");
                }
            });
        });
    });