簡體   English   中英

Ajax請求不適用於ASP.NET頁面方法

[英]Ajax request is not working with ASP.NET page method

我嘗試了很多示例代碼,但均無濟於事。 Ajax調用根本不返回任何內容。 沒有警報彈出窗口。

這是ajax代碼:

<script src="Scripts/jquery-1.10.2.js" type="text/javascript"></script>
<script type="text/javascript"> 
    $(document).ready(function() {
        $.ajax({         
            type: "POST",
            url: "mysqlcall.aspx/Testing",
            contentType: "application/json; charset=utf-8",
            data: {""},
            dataType: "json",
            success: function (data) {
                alert("idkbro");
                //  $("#testing").text(response.d);
            },
            failure: function (response) {
                alert(response.d);
            }
        });
    });
</script>

這是呼叫頁面:

public partial class mysqlcall : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {    
    }

    public string Testing()
    {
        return "asdlasldalsdl";
    }
}

我在最新版本的IIS上運行它,並使用帶路由的asp.net C#Webforms。 mysqlcall.aspx沒有被路由,但是沒關系我嘗試了路由頁面。 任何幫助,將不勝感激。

編輯:感謝@wazz,問題出在

url: "mysqlcall.aspx/Testing",

相反,它必須以斜杠開頭:

url: "/mysqlcall.aspx/Testing",

添加WebMethod屬性並使該方法static

[WebMethod]
public static string Testing()
{
    return "asdlasldalsdl";
}

您還可以從ajax調用中刪除data ,或刪除引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM