我收到了错误
**POST http://localhost:34169/createNew.aspx.cs/Confirm 403 (Forbidden) **
当我尝试使用Jquery AJAX调用CodeBehind函数时。
我的代码:
function CallConfirmMethod(str) {
$.ajax(
{
type: "POST",
contentType: "application/json; charset=utf-8",
url: "createNew.aspx.cs/Confirm",
data: "{'smallPos': " + str + "}",
success: function (result) { alert("successful!"); }
});
}
CodeBehind函数(实际上并没有做任何事情,只是为了测试一下):
[System.Web.Services.WebMethod(BufferResponse = false)]
protected void Confirm(string str) {
// SKICKA SQL-QUERY
Response.Write("Funktionen kallas! " + str);
}
答案 0 :(得分:2)
我认为您的错误在url createNew.aspx.cs /确认您应将其更改为 createNew.aspx / Confirm 。这篇文章也很好Calling ASP.Net WebMethod using jQuery AJAX
答案 1 :(得分:0)
也许你得到500,因为你没有关闭响应
[System.Web.Services.WebMethod(BufferResponse = false)]
protected void Confirm(string str) {
// SKICKA SQL-QUERY
Response.Clear();
Response.ContentType = "application/text";
Response.StatusCode = 200;
Response.Write("Funktionen kallas! " + str);
Response.Flush();
Response.End();
}
如果你想在json中发送回复,这是文章AJAX to web method not returning JSON