I am trying to pass a variable to a web method using Jquery with ajax. I am confused with the syntax to pass the variable and have tried many different forms with no success.

The webmethod is:

public string GetStudentName(string studentID)
{
    string name = string.Empty;
    int id = 0;

    // convert the string to an integer
    id = int.Parse(studentID);

    // If the studentID is withinrange 
    if (id < 0 || id >= _StudentList.Count)
    {
        name = "Not Found";
    }
    else
    {
        name = _StudentList[id].LastName + ", " + _StudentList[id].FirstName;
    }

    return name;
}

The student list is populated earlier in the code.

The jQuery code is:

$('#cmdLookup').click(function ()
{
    var sid = $("#<%=txtID.ClientID%>").val();

    $.ajax({

       type: "POST",

       url: "Services/WSStudent.asmx/GetStudentName",

       contentType: "application/json; charset=utf-8",

       data: sid,

       dataType: "json",

       success: function (result)
       {
           $('#<%=txtStudentName.ClientID%>').text(result.d);
       },
       error: function (XMLHttpRequest, textStatus, errorThrown)
       {
           alert('Error: ' + XMLHttpRequest.responseText);
       }
   })
})

If someone could show me the right syntax for passing the sid variable that would be great.

Related posts

Recent Viewed