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

标签 javascript c# asp.net ajax

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

     }

Page.ascx:

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

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

}

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

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");

}

关于javascript - asp.net 将 javascript ajax 发送到用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31160052/

相关文章:

c# - 无法更新 .mdf 数据库,因为数据库是只读的(Windows 应用程序)

javascript - 单击按钮发送 Ajax 调用并从 aspx.cs(代码隐藏)获取值到 aspx 页面并将其显示在同一页面的输入字段中

asp.net - silverlight 错误 - 相对 uri 不支持该操作

JavaScript 错误 : Unable to get property 'style' of undefined or null reference?

c# - 学习创建自定义 Silverlight 组件

javascript - 使用 javascript 的 xml 属性缺少结束标记

javascript - Angular js http 没有捕获 promise 错误

javascript - 如何以正确的方式在 Angular.js 查询服务中传递参数?

c#文件路径字符串比较不区分大小写

javascript - 如何检查img src是否加载