使用jQuery和AJAX传递参数给VB.NET的webmethod参数、AJAX、jQuery、webmethod

2023-09-10 17:54:09 作者:心痒改不了信仰

我一直在网上搜索了几个小时试图将参数传递到使用jQuery $就背着我code。我试过一吨不同的东西,但没有奏效。当我不传递任何参数,并设定vb.net功能不接收参数的功能将被调用。但是,一旦我尝试添加参数,函数永远不会被调用。

客户端:

  $(#<%= saveResource2.clientID%>中)。点击(函数(){
        VAR parDesc = $(#<%= ddlPDesc.clientID%>选项:选择)。text()的;
        $(#<%= Button1.clientID%>中)。点击();
        $阿贾克斯({
            键入:POST,
            网址:Projects.aspx / btnSaveResource
            数据:JSON.stringify({说明:parDesc}),
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:JSON,
            成功:函数(MSG){

                $(#<%= lblPerson.clientID%>中)。文(MSG);
                //做一些有趣的事情在这里。
            }
        });

    });
 

服务器端:

 <的WebMethod()> _
< ScriptMethod(ResponseFormat:= ResponseFormat.Json)> _
公共共享功能btnSaveResource(BYVAL parDesc作为字符串)作为字符串
    昏暗的Ð作为字符串= parDesc
    返回D +测试
 端功能
 

解决方案

尝试从这个不断变化的:

 数据:JSON.stringify({说明:parDesc}),
 
JQuery Ajax

 数据:JSON.stringify({parDesc:parDesc}),
 

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

解决方案

Try changing from this:

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

To

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