首页 > 解决方案 > 如何使用 ajax 将 id 从客户端传递到 webform

问题描述

我的代码不起作用,我不知道为什么。

我的ajax代码调用:

    var id = $(this).attr("data-id")
    $.ajax({
        type: "POST",
        url: "XTM307.aspx/GetPart",
        data: { "id": id },
        contentType: "application/json",
        dataType: "json",
        success: function (response) {

        }
    });

我的服务器代码:

<System.Web.Services.WebMethod()>
<ScriptMethod(UseHttpGet:=True, ResponseFormat:=ResponseFormat.Json)>
Public Shared Sub GetPart(id As String)
    Dim test = 2
    test += 3
End Sub

标签: asp.netajax

解决方案


您的网络服务方法是GET,所以 ajax 调用应该是这样的:

var id = $(this).attr("data-id")

$.ajax({
    type: "GET",
    url: "XTM307.aspx/GetPart?id=" + id,
    contentType: "application/json",
    dataType: "json",
    success: function (response) {

    }
});

推荐阅读