簡體   English   中英

Ajax POST未調用Codebehind函數

[英]Codebehind function not being called by Ajax POST

我的Ajax帖子沒有在方法后面運行我的代碼,因此不返回任何數據。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="AspTest.Test" %>

Test.aspx(Ajax腳本)

    <script>
                $(".avatarThumb").click(function () {
                    $.ajax({
                        type: "POST",
                        url: "Test.aspx/MyMethod",
                        //data: {"s": "Some data passed through" },
                        //contentType: 'application/json; charset=utf-8',
                        //dataType: 'json',
                        success: function (response) {
                            alert(response.d); //Returns "undefined"
                        },
                        failure: function(response) {
                            alert(response);
                        }
                    });
                });
            </script>

刪除contentType和dataType確實可以成功,但是不會運行codeBehind方法。 如果contentType和/或dataType處於活動狀態,它將不會成功或失敗。 FF firebug不顯示任何錯誤。

* Test.aspx.cs CodeBehind方法

        [System.Web.Services.WebMethod]
        public static string MyMethod()
        {
(*)         Debug.WriteLine("MyMethod called!"); // (*) Breakpoint never reached

            return "Method called!";
        }

像這樣更改您的代碼。 它應該工作正常

var SendData = {};
$.ajax({
        type: "POST",
        url: "Test.aspx/MyMethod",
        data: JSON.stringify(SendData),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            alert(data.d);
        },
        error: function (result) {
            console.warn(result.statusText);
        }
    });

您的C#方法:

[System.Web.Services.WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string MyMethod()
{

    return "Method called!";
}

如果您的頁面Test.aspx在項目的根目錄下,則上面的代碼有效。 如果沒有,請根據文件的位置深度更改url參數值,例如“ ../Test.aspx/MyMethod”。

如下更改數據並發送ajax。

   $.ajax({
                    type: "POST",
                    url: "Test.aspx/MyMethod",
                    data: JSON.stringify({"s": "Some data passed through" }),
                    contentType: 'application/json; charset=utf-8',
                    //dataType: 'json',
                    success: function (response) {
                        alert(response.d); //Returns "undefined"
                    },
                    failure: function(response) {
                        alert(response);
                    }
                });

現在,web方法應該與通過ajax發送的參數匹配。

    [System.Web.Services.WebMethod]
    public static string MyMethod(string s)
    {

        return "Method called!";
    }

嘗試這個:

$(".avatarThumb").click(function () {
    $.ajax({
        type: "POST",
        url: '<%=ResolveUrl("~/Test.aspx/MyMethod")%>',
        contentType: 'application/json; charset=utf-8',
        success: function (response) {
            alert(response.d); 
        },
        failure: function(response) {
            alert(response);
        }
    });
});

確認類型GET或POST。

暫無
暫無

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

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