我有一些代码。我想添加 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");
}
这是我的错误:
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code
最佳答案
您的代码中有几个错误
- [WebMethod] 应该是静态的才能调用它。
- 将 btnGOlustur_Click 逻辑移至单独的静态方法
- 您无法在 ajax 上下文中进行 Response.Redirect
- 从 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");
}
});
});
});
关于c# - 如何同时使用 jquery、ajax 和 mysql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23627708/