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/