如何通过ajax

时间:2016-06-29 03:59:03

标签: c# jquery ajax vb.net parameters

我需要将参数从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()As String),那么它将正常工作。然后我如何传递参数?< / p>

1 个答案:

答案 0 :(得分:1)

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

<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