neethu
neethu

Reputation: 193

How to pass parameter to a function defined in vb.net through ajax

I need to pass parameters from an ajx call to a function defined in vb.net.

The function definition is:

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


        Return "jhgfjhf"

    End Function

Ajax call is as below:

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"); }
            }
        });


    });

While using this code resulting to an internal server error.If I remove argument section (used data:{} and Public Shared Function wwww() As String),then it will work fine.Then how can I pass parameters?

Upvotes: 1

Views: 1363

Answers (1)

Matteo Umili
Matteo Umili

Reputation: 4017

To allow calls from script you need to add the ScriptService attribute to your WebService, then (to return a JSON) add ScriptMethod attribute to the 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

Then you need to slightly modify how you pass data through javascript like this:

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

Value will be returned in JSON, so to read the value you'll need to:

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

Upvotes: 1

Related Questions