我正在尝试使用ASP.NET JQUERY AJAX调用更新数据
但它不会更新数据。
如何解决此错误?
的Javascript
<script type="text/javascript">
//Guncelleme Alanı Oluştur
function guncellemeolustur(id) {
$.ajax({
type: "POST",
url: "AsynUygulama.aspx/guncellemealani",
data: "{id:'" + id + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#" + id).fadeOut("slow", function () {
$(this).html(msg.d)}).fadeIn("slow");
}
});
}
HTML标记
<div>
<h2>Departman Tablosu</h2>
<table border="1">
<thead>
<th>Departman Adı</th>
<th style="text-align: center;">İşlem</th>
<th style="text-align: center;">İşlem</th>
</thead>
<tbody>
<asp:Repeater ID="Repeat1" runat="server">
<itemtemplate>
<tr id='<%# Eval("ID") %>'>
<td><%# Eval("Ucret") %> - <%# Eval("ParaTipi") %></td>
<td><%# Eval("YasGrubu") %></td>
<td>
<input type="button" onclick="guncellemeolustur(<%# Eval("ID") %>)" value="Güncelle" />
<input type="button" id="silbtn" style="margin-left: 10px;" onclick="silkayit(<%# Eval("ID") %>)" value="Sil" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
</div>
WebMethod
[System.Web.Services.WebMethod]
public static string guncellemealani(Int32 id)
{
SiteDB system = new SiteDB();
string sonuc = "";
SqlConnection bg = system.baglan();
SqlCommand komut = new SqlCommand("select * from TurCocukFiyat where ID=@id", bg);
komut.Parameters.AddWithValue("@id", id);
bg.Open();
SqlDataReader dr = komut.ExecuteReader();
dr.Read();
sonuc = "<input id=guncelle type=text value=" + dr["TurAdi"].ToString() + " /><input type=button value=Güncelle onclick=guncelleislemi(" + id + ",document.getElementById('guncelle').value) />";
bg.Close();
return sonuc;
}
执行更新的代码:
Javascript
//Guncelleme Islemi Gerceklestirir
function guncelleislemi(id, kayit) {
$.ajax({
type: "POST",
url: "AsynUygulama.aspx/guncellemeyap",
data: "{kayit:'" + kayit + "', id:'" + id + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
usercontrolgetir("AsynUserControl.ascx");
}
});
}
WebMethod
[System.Web.Services.WebMethod]
public static string guncellemeyap(Int32 id, string kayit)
{
SiteDB system = new SiteDB();
SqlConnection bg = system.baglan();
SqlCommand komut = new SqlCommand("update TurCocukFiyat set ID='" + id + "' and BirimAdi=" + kayit, bg);
bg.Open();
komut.ExecuteNonQuery();
bg.Close();
return "guncelleme basarili";
}
答案 0 :(得分:0)
尝试下面的代码,请传递没有引号的id值,因为webmethod中的接受参数类型为int
function guncellemeolustur(id) {
$.ajax({
type: "POST",
url: "AsynUygulama.aspx/guncellemealani",
data: "{id:" + id + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#" + id).fadeOut("slow", function () {
$(this).html(msg.d)}).fadeIn("slow");
}
});
}
答案 1 :(得分:0)
我建议使用以下代码添加jquery ajax错误回调:
function guncellemeolustur(id) {
$.ajax({
type: "POST",
url: "AsynUygulama.aspx/guncellemealani",
data: "{id:" + id + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#" + id).fadeOut("slow", function () {
$(this).html(msg.d)}).fadeIn("slow");
}
error: function(jqXHR){
glb = jqXHR; //creates a global var with the response from server
}
});
}
现在您可以从浏览器开发人员工具控制台检查glb。 看一下glb.responseText。它会给你从服务器返回的错误。 如果错误没有帮助你请粘贴在这里。也许它会帮助我\用户分析。