【问题标题】:How to pass parameter to a function defined in vb.net through ajax如何通过ajax将参数传递给vb.net中定义的函数
【发布时间】:2016-06-29 03:59:02
【问题描述】:

我需要将 ajx 调用中的参数传递给 vb.net 中定义的函数。

函数定义为:

<System.Web.Services.WebMethod()> _
    Public Shared Function wwww(ByVal id As String) As String


        Return "jhgfjhf"

    End Function

Ajax 调用如下:

var l = window.location;
    var base_url = l.protocol + "//" + l.host;

    $(".pagen ").click(function () {


        var num = $(this).attr('id');

        alert(num);
        $.ajax({
            type: "POST",
            url: base_url + '/Album%20Viewer%20web/albumlist.aspx/wwww',
            data: { id:num },
            dataType: 'json',
            async: false,
            cache: false,
            contentType: "application/json",

            success: function (response) {

                console.log(response);




            },
            error: function (jqXHR, textStatus, errorThrown) {

                if (typeof (console) != 'undefined') {
                    console.log(errorThrown);
                }
                else { alert("something went wrong"); }
            }
        });


    });

使用此代码导致内部服务器错误。如果我删除参数部分(使用的数据:{} 和公共共享函数 wwww() 作为字符串),那么它会正常工作。那么我该如何传递参数?

【问题讨论】:

    标签: c# jquery ajax vb.net parameters


    【解决方案1】:

    要允许来自脚本的调用,您需要将 ScriptService 属性添加到 WebService,然后(返回 JSON)将 ScriptMethod 属性添加到 WebMethod

    <ScriptService()>
    Public Class WebService1
        Inherits System.Web.Services.WebService
    
        <WebMethod()> _
        <ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
        Public Function wwww(ByVal id As String) As String
            Return id & "AAA"
        End Function
    
    End Class
    

    然后您需要稍微修改一下通过 javascript 传递数据的方式,如下所示:

    data: "{ 'id':'" + num +"'}", // "{'id':'something'}"
    

    值将以 JSON 格式返回,因此要读取您需要的值:

    var returnedValue = response.d // 'd' because Microsoft decided so
    

    【讨论】:

      猜你喜欢
      • 2017-04-14
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 2014-05-02
      • 1970-01-01
      相关资源
      最近更新 更多