gpt4 book ai didi

c# - jquery POST 与经典 asp 的结合

转载 作者:太空宇宙 更新时间:2023-11-03 14:35:04 25 4
gpt4 key购买 nike

我已经尝试了很多方法来执行这个但是它没有发生..这是交易..我在表格行中显示数据,每一行都有它的id代表我想删除的人的id。我使用 jquery 从我的按钮所在的行收集此 id,这是我如何执行此操作的代码,我正在编写它的工作原理,以便您可以深入了解我要完成的工作:

var customer_id = $(this).parents("tr").attr('id');

当我使用 alert 测试它时它可以工作,这是困难的部分我只是卡住了我有一个名为 Delete.aspx 的文件这是它的内容

<%

string p = Request["value"]; int pInt = Int32.Parse(p);

var dataContext = new CustomersDataContext();
var customer = from m in dataContext.Customers
where m.id == pInt
select m;
dataContext.Customers.DeleteAllOnSubmit(customer);
dataContext.SubmitChanges();
%>

现在我正在尝试将值发送到 Delete.aspx 以删除具有特定 ID 的人,它通过在浏览器中键入 Delete.aspx?value=7 来工作,它会删除 ID 为 7 的人。现在这里是我真的被卡住了。我试图从 Default.aspx 到达 Delete.aspx 并使用 jquery 传递人员 ID,如下所示:

$(".btn-delete").click(function() {
var answer = confirm("If you press OK, this customer will be deleted?")
var customer_id = $(this).parents("tr").attr('id');
if (answer) {


$.ajax({
type: "POST",
url: "Delete.aspx",
data: "{value: '" + customer_id + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
AjaxSucceeded(msg);
},
error: AjaxFailed

});

$(this).parents("tr").animate({ backgroundColor: "#fbc7c7" }, "fast")
.animate({ opacity: "hide" }, "slow")
return false;
}
else {
alert("Customer has not been deleted!")
}


});

function AjaxSucceeded(result) {
alert(result.d);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}

});

现在的结果是,当我单击按钮并确认删除时,我得到“500 内部服务器错误”,这是可以修复的还是有其他简单的方法可以做同样的事情?

谢谢


我已经修改了代码..但我仍然需要一些帮助..我觉得我很接近所以至少给我指出正确的方向..

这是我的 Delete.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Second_Question
{
public partial class Delete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

string p = Request["value"];
int pInt = Int32.Parse(p);

var dataContext = new CustomersDataContext();
var customer = from m in dataContext.Customers
where m.id == pInt
select m;
dataContext.Customers.DeleteAllOnSubmit(customer);
dataContext.SubmitChanges();


}
}
}

这是我的 Delete.aspx

有什么想法吗?谢谢

最佳答案

“500 内部服务器错误”通常是因为您有一些服务器端代码未正确编译或引发未处理的异常。

我建议您尝试一些更改:

将删除例程移动到用 [WebMethod] 属性修饰的特定方法。确保您的代码包含 System.Web.Services。

让您的 jQuery ajax 调用包括“/Delete.aspx/MyDeleteMethod”而不是整个页面。

您是否考虑过此应用程序的安全性?如果有人捕获了您的 customer_id 并使用 Firebug 即时更改它会怎样?

编辑:好的,这是我对服务器端代码的建议

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services

namespace Second_Question
{
public partial class Delete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

[WebMethod]
public static void DeleteCustomer(int CustID)
{

var dataContext = new CustomersDataContext();
var customer = from m in dataContext.Customers
where m.id == CustID
select m;
dataContext.Customers.DeleteAllOnSubmit(customer);
dataContext.SubmitChanges();


}
}
}

我的 jQuery 看起来像这样:

$.ajax({
type: "POST",
url: "Delete.aspx/DeleteCustomer",
data: "{CustID: " + parseInt(customer_id) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
AjaxSucceeded(msg);
},
error: AjaxFailed
});

关于c# - jquery POST 与经典 asp 的结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1408213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com