繁体   English   中英

jQuery POST与经典ASP的组合

[英]Jquery POST combination with classic asp

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

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

当我使用Alert对其进行测试时,这是困难的部分,我只是被卡住了,我有一个名为Delete.aspx的文件,这是其内容

<%

字符串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 Internal server error”,这是可修复的,或者还有其他做同一件事的简单方法了吗?

谢谢


我已经修改了代码..但仍然需要一些帮助..我觉得我是如此亲密,至少可以将我指向正确的方向..

这是我的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 Internal Server Error”,因为您有一些服务器端代码未正确编译或引发了未处理的异常。

我建议您可能要尝试一些更改:

将删除例程移到[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
 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM