c# - Asp net AJAX 在 MasterPage 中

标签 c# asp.net ajax

我试图在我的 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/

相关文章:

javascript - ASP.NET MVC/Javascript : VideoJS media could not be loaded

c# - 从 ZipFile 中检索文件名

c# - 将图像转换为 CSV

c# - ASP.NET 5 : How to reload strongly typed configuration on change

javascript - $.ajax 结合 $.ajaxSetup 默认设置

c# - Asp.Net Mvc POST 方法不发送字符串

javascript - ajax请求后将参数传递给 Angular 组件

c# - Ocelot - 更改网关中的上游请求主体不会导致下游请求发生变化

c# - 使用表单元素与 UserControls 交互

asp.net - 在每个 Controller 的ASP.NET WebAPI上强制使用CamelCase