C# 如何将jquery、ajax和mysql结合使用?
我有一些代码。我想在我的代码中添加jquery和ajaxin。我试过了,但没有这样做 这里是我的jQuery代码C# 如何将jquery、ajax和mysql结合使用?,c#,jquery,mysql,ajax,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();
$(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”类型的异常,但未在用户代码中处理
您的代码中有几个错误
[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");
}
});
});
});
当我在这里使用static时,combobox和Session出现错误。@user3627624,请查找更新的答案。您还需要使用
我仍然有一些错误,这三行int Pro_id=HttpContext.Current.Request.Params[“project_id”];int Sta_id=转换为NT32(下拉选择值);int-Ity\u-id=转换为32(drop\u i.SelectedValue);它没有看到组合框。@user3627624,您无法看到组合框,因为它没有发送viewstate,并且没有遵循整个页面生命周期。如果您不确定,请阅读有关使用jQuery和aspx页面方法的更多信息。还有一些将字符串转换为int的基本c#编程工具,等等,我的错误是,“非静态字段方法或属性需要一个对象引用。它在combobox中工作,但在静态中不工作。为什么?