Hi,
I am calling web service through jquery ajax call for the first time and getting an error. Can please somebody help me in this i would really appreciate it. Below is the code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="userNTID.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script src="jquery.js" type="text/javascript"></script>

    <script src="JScriptuserNTID.js" type="text/javascript"></script>
    
</head>
<body>
    <form id="form1" runat="server">
    <div>
Employee's NTID: <input type="text" id = "Eid" name="Employee_NTID" />
<br />
<br />
    <button type = "button" id = "callingMethod" class ="start">Search</button> 
    </div>
    </form>
</body>
</html>

Here is JScriptuserNTID.js file:

$.ajaxSetup({
    cache: false
});
//var ajax_load = "<img src='loading.gif' alt='loading...' />";

//  load() functions
//var loadUrl = "test.txt";

$(document).ready(function() {
$('.start').ajaxStart(function() {
        alert('Triggered ajaxStart handler.');
    });
    $("button").click(function() {
    //$("p").html(ajax_load).load(loadUrl);
    //var eNtid = $("#Eid").val();
    $.ajax({
        type: "POST",
        url: "./WebService.asmx/HelloWorld",
        //data: ("Employee NTID :" + eNtid),
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        },
        success: function(result) {
            alert("success");
        }
    }); 


    });
});

Here is WebService.asmx/HelloWorld:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public void HelloWorld() {
        Console.WriteLine( "Hello World" );
    }
    
}

Hi,
I am now using .aspx.cs file for method but still getting the same error. i m new to web development. please somebody help me in code and give me correct guidance. Thanks

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
     //using System.Web.Services; 
    [WebMethod()] 
    //[ScriptMethod()] 

    public static string SendMessage(string subject, string message, string messageId, string pupilId) 
    {
        return "Hello world";

        //send message 
    } 
}

and .js file is now:

$.ajaxSetup({
    cache: false
});
//var ajax_load = "<img src='loading.gif' alt='loading...' />";

//  load() functions
//var loadUrl = "test.txt";

$(document).ready(function() {
$('.start').ajaxStart(function() {
        alert('Triggered ajaxStart handler.');
    });
    $("button").click(function() {
    //$("p").html(ajax_load).load(loadUrl);
    //var eNtid = $("#Eid").val();
    $.ajax({
        type: "POST",
        url: "userNTID.aspx/SendMessage",
        //data: ("Employee NTID :" + eNtid),
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        },
        success: function(result) {
            alert("success"+result);
        }
    }); 


    });
});
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.