1

I am creating a hospital ERP project for my office. I have already added some features. in this time I need jquery ajax request for my project. I'm trying more but I am not a success this webform. so I need help expert person. My Code Not working asp.net webform jquery ajax request.

Here is my code

[System.Web.Services.WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Employee GetByEmployeeId(int Id)
{
    Employee employee = new Employee();
    return employee;
}

<script src="Scripts/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#saveButton').click(function () {
            debugger;
            $.ajax({
                type: 'POST',
                url: 'Test.aspx/GetByEmployeeId',
                data: '{Id: ' + 1 + '}',
                success: function (respons) {
                    debugger;
                    alert('ok');
                },
                error: function () {
                    alert('fail')
                }
            })
        })
    })
</script>


  <div class="form-group">
    <asp:Label runat="server" For="nametextBox">Name:</asp:Label>
    <asp:TextBox runat="server" ID="nametextBox" CssClass="form-control"> 
 </asp:TextBox>
</div>

<div class="form-group">
    <button type="submit" class="btn btn-primary" id="saveButton">Save</button>
</div>

I am trying this but not working

3 Answers3

0

use static keyword in your web service.

public static Employee GetByEmployeeId(int Id){
Employee employee = new Employee();
return employee;}
0

Use this after debugger and then test what the service returns and their are not any employee on id=1 which you are passing. Thats why it returns null or error. var res = JSON.stringify(response); var obj = JSON.parse(res);

0

First of all, make your method static as below:

public static Employee GetByEmployeeId(int Id)

to call a method using ajax you have to make the method static which is must.

then change button type from submit to button. secondly use:

$('#saveButton').on('click', function () {

instead of:

$('#saveButton').click(function () {
Muhammad Aftab
  • 1,098
  • 8
  • 19