0

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.

1
  • 2
    How is this asp-classic? Looks more like ASP.NET to me. Commented Mar 21, 2016 at 12:32

1 Answer 1

1

Try data: { studentID: sid } and make sure that sid variable has the value you want

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.