Using Jquery and AJAX to pass parameters to VB.NET webmethod

江枫思渺然 提交于 2019-12-21 20:49:15

问题


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}),


来源:https://stackoverflow.com/questions/11351301/using-jquery-and-ajax-to-pass-parameters-to-vb-net-webmethod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!