Jquery POST与经典asp的组合

问题描述:

我已经尝试了很多方法来执行这个,但它没有发生..这里是交易..我有数据显示在表行中,每一行都有它的id代表我想删除的人的id 。我使用jquery从我的按钮所在的行收集此ID,这里是代码我是如何做到的,它的工作我只是写作,所以你可以得到我想要实现的内容的洞察:Jquery POST与经典asp的组合

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

当使用它的工作原理警告我测试,这里是困难的部分,我只是坚持我有文件名为Delete.aspx这里是它的内容

<% 

列p =请求[“价值”]; 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?值= 7,将删除与人ID 7现在这里是我真正陷入困境的地方。从Default.aspx的我试图达到Delete.aspx并通过使用个人ID的jQuery这样的:

$(".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

什么想法?谢谢

+0

这不是典型的ASP - 它看起来像C#一样。你确定你的问题和标签正确吗? – 2009-09-10 23:23:09

+0

服务器端代码是否发生在Page_Load方法中? – 2009-09-10 23:23:49

+0

我不太确定..我刚刚几天前开始使用asp,我也会添加该标记。 – ant 2009-09-10 23:24:38

“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 
}); 
+0

你能详细说明这个“有你的jQuery ajax调用包括”/Delete.aspx/ MyDeleteMethod“而不是整个页面。” ?谢谢 – ant 2009-09-10 23:37:44

+0

我添加的例子展示了jQuery调用Delete.aspx中特定的专门构建的web方法,而不是直接调用页面。通过这种方式,您可以更加逻辑地将您的担忧分开。 – 2009-09-11 00:42:03

+0

太棒了,它的作品:)谢谢 – ant 2009-09-11 00:44:51