Using Jquery and AJAX to pass parameters to VB.NET

2019-04-11 21:47发布

问题:

I've been searching the internet for hours trying to pass parameters to my code behind using JQUERY $.ajax. I've tried a ton of different things, but nothing has worked. When I don't pass any parameters and set the vb.net function to not receive parameters the functions will get called. But once I try adding parameters, the function never gets called.

Client Side:

$("#<%=saveResource2.clientID %>").click(function() {
        var parDesc = $("#<%=ddlPDesc.clientID %> option:selected").text();
        $("#<%=Button1.clientID %>").click();
        $.ajax({
            type: "POST",
            url: "Projects.aspx/btnSaveResource",
            data: JSON.stringify({Desc: parDesc}),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {

                $("#<%=lblPerson.clientID %>").text(msg);
                // Do something interesting here.
            }
        });

    });

Server Side:

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function btnSaveResource(ByVal parDesc As String) As String
    Dim d As String = parDesc
    Return d + "test"
 End Function

回答1:

Try changing from this:

data: JSON.stringify({Desc: parDesc}),

To

data: JSON.stringify({parDesc: parDesc}),