我试图在我的 MasterPage 文件中使用一些 AJAX,但出现了这个错误:
http://localhost:15354/website/MasterPage.master/Login Failed to load resource: the server responded with a status of 403 (Forbidden)
我在一些论坛上发帖说我们不能像这样从 MasterPage 调用带有 AJAX 的函数:
function Login() {
var obj = new Object();
obj.user = "";
obj.pass = "";
$.ajax({
type: "POST",
url: "MasterPage.master/Login",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert("success");
},
error: function (){
alert("error");
}
});
}
这是我在后面的代码中的功能:
[WebMethod]
public static string Login(string user, string pass)
{
return "";
}
我该怎么做?
非常感谢!
最佳答案
最好为此使用 web 服务并从 ajax 请求中调用 web 服务 url。
$.ajax({
/// <summary>
/// Perform an asynchronous HTTP (Ajax) request
/// </summary>
type: "POST",
url: "AjaxServer.asmx/Login",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
/// <summary>
/// when success
/// </summary>
alert(response.d);
},
failure: function(msg) {
/// <summary>
/// when there is an error
/// </summary>
alert(msg.d);
}
});
关于c# - Asp net AJAX 在 MasterPage 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9756426/