【问题标题】:How can I use together jquery,ajax and mysql?如何同时使用 jquery、ajax 和 mysql?
【发布时间】:2014-06-30 22:01:59
【问题描述】:

我有一些代码。我想在我的代码中添加 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");
}

这是我的错误:

在 mscorlib.dll 中发生了“System.Threading.ThreadAbortException”类型的第一次机会异常
mscorlib.dll 中出现“System.Threading.ThreadAbortException”类型的异常,但未在用户代码中处理

【问题讨论】:

    标签: c# jquery mysql ajax


    【解决方案1】:

    您的代码中有几个错误

    1. [WebMethod] 应该是静态的才能调用它。
    2. 将 btnGOlustur_Click 逻辑移动到单独的静态方法中
    3. 您不能在 ajax 上下文中进行 Response.Redirect
    4. 从 Request.Params 中挂钩值

    综合以上几点

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

    【讨论】:

    • 当我在这里使用静态时,组合框和会话出现错误。
    • @user3627624,请找到更新后的答案。您还需要使用<asp:ScriptManager EnablePageMethods=true />。阅读此encosia.com/…
    • 这三行我还有一些错误 int Pro_id = HttpContext.Current.Request.Params["project_id"]; int Sta_id = Convert.ToInt32(drop_s.SelectedValue); int Ity_id = Convert.ToInt32(drop_i.SelectedValue);它没有看到组合框。
    • @user3627624,您看不到组合框,因为它没有发送视图状态,也没有遵循整个页面生命周期。如果您不确定,请阅读有关使用 jQuery 和 aspx 页面方法的更多信息。还有一些基本的 c# 编程东西来将字符串转换为 int 等
    • 我的错误是,“非静态字段方法或属性需要对象引用。它在组合框中工作,但在静态不工作。为什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 2013-03-11
    相关资源
    最近更新 更多