【问题标题】:Jquery POST combination with classic aspjquery POST 与经典asp的组合
【发布时间】:2009-09-10 23:16:48
【问题描述】:

我已经尝试了很多执行此操作的方法,但它没有发生.. 这是交易.. 我有数据显示在表格行中,每一行都有它的 id 代表我要删除的人的 id。我使用 jquery 从我的按钮所在的行中收集这个 id,这是我如何做到这一点的代码,它的工作我只是在写这样你就可以了解我想要完成的事情:

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

当我使用 alert 对其进行测试时,它可以工作,这是困难的部分,我只是卡住了我有一个名为 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?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

有什么想法吗?谢谢你

【问题讨论】:

  • 这不是经典的 ASP - 它看起来像 C#。您确定您的问题和标签措辞正确吗?
  • 服务器端代码是否发生在Page_Load方法中?
  • 我不确定..我几天前开始使用asp,我也会添加那个标签..
  • 服务器端代码是否发生在 Page_Load 方法中 - 我不知道..
  • protected void Page_Load(object sender, EventArgs e) { } - 我找到了 .. 我猜它没有发生 .. inside page load

标签: c# asp.net jquery post


【解决方案1】:

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

【讨论】:

  • 您能否详细说明一下“让您的 jQuery ajax 调用包含“/Delete.aspx/MyDeleteMethod”而不是整个页面。” ?谢谢
  • 我添加的示例显示 jQuery 在 Delete.aspx 中调用特定的、专门构建的 Web 方法,而不是直接调用页面。这样,您可以更合乎逻辑地分离您的关注点。
猜你喜欢
  • 2011-04-27
  • 2011-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-25
  • 2018-10-22
相关资源
最近更新 更多