繁体   English   中英

asp.net 将 javascript ajax 发布到用户控件

[英]asp.net post javascript ajax to usercontrol

Javascript:

function Proposal(GetProposal, ProductName, ProductID) {

      $.ajax({
      type: "POST",
      url: "Page.ascx/Proposal",
      data: JSON.stringify({ GetProposal: GetProposal, ProductName: ProductName, ProductID: ProductID }),
      contentType: "application/json; charset=utf-8",
      dataType: "json",

      failure: function (response) {
          alert(response.d);
                                   }
     });

     }

页面.ascx:

[WebMethod]
public static void Proposal(string GetProposal, string ProductName, string ProductID)
{

HttpContext.Current.Response.Redirect("MyPage");

}

当我尝试将 ajax 发布到 Proposal 方法时,出现以下错误:

POST http://localhost:63290/Page.ascx/Proposal 403 (Forbidden)

我想念我的代码中要更改的内容?

您不能调用此方法,因为 ascx 控件不代表可从客户端计算机访问的真实 URL。 它们是服务器端,旨在嵌入其他页面。

相反,您可以尝试将您的方法放在.aspx页面中,然后调用方法

$.ajax({
      type: "POST",
      url: "NewPage.aspx/Proposal",
      data: JSON.stringify({ GetProposal: GetProposal, ProductName: ProductName, ProductID: ProductID }),
      contentType: "application/json; charset=utf-8",
      dataType: "json",

      failure: function (response) {
          alert(response.d);
                                   }
     });

并且您的NewPage.aspx必须包含您编写的相同方法

[System.Web.Services.WebMethod]
public static void Proposal(string GetProposal, string ProductName, string ProductID)
{

     HttpContext.Current.Response.Redirect("MyPage");

}

暂无
暂无

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

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