asp.net向用户控制发布javascript ajax
本文关键字:javascript ajax 控制 net 用户 asp | 更新日期: 2023-09-27 18:00:35
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");
}