Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am unable to call the webmthod from javascript using ajax call. Please check the code. What mistake i have done?

Filepaths:

C:\Users\Abhijit\Documents\Visual Studio 2013\WebSites\UpdatePanel\App_Code\DataService.cs

C:\Users\Abhijit\Documents\Visual Studio 2013\WebSites\UpdatePanel\Pages\Default.aspx

C:\Users\Abhijit\Documents\Visual Studio 2013\WebSites\UpdatePanel\Scripts\jquery-1.10.2.js
C:\Users\Abhijit\Documents\Visual Studio 2013\WebSites\UpdatePanel\Scripts\Test.js

What I have tried:

Default.aspx

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../Scripts/jquery-1.10.2.js"></script>
    <script src="../Scripts/Test.js"></script>
</head>
<body>

ASP.NET
<form id="form1" runat="server">

HTML
        <input id="btnTest" type="button" onclick="CallFunction()" />
    </form>
</body>
</html>


-------------------------------------------------------------------------
Test.js

JavaScript
function CallFunction()
{

    $.ajax({
        type: "POST",
        url: "../DataService/GetData",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var names = response.d;
            alert(names);
        },
        failure: function (response) {
            alert(response.d);
        }
    });
}


---------------------------------------------------------------------------------
DataService.cs

C#
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;

/// <summary>
/// Summary description for DataService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class DataService : System.Web.Services.WebService
    {

        [WebMethod (EnableSession=true)]
        public string GetData()
        {
            Dictionary<string, string> name = new Dictionary<string, string>();
            name.Add("1", "Sourav Kayal");
            name.Add("2", "Ram mishra");
            string myJsonString = (new JavaScriptSerializer()).Serialize(name);
            return myJsonString;
        }
    }



-------------------------------------------------------------------------------------
Posted
Updated 17-Dec-16 9:28am
Comments
Afzaal Ahmad Zeeshan 17-Dec-16 12:14pm    
Does it work? What error did you get?
Abhijit Parab 17-Dec-16 13:28pm    
It didn't work. I didn't get any error. When I was debugging $.ajax method got called but it didn't call WebMethod. No success or failure.
Afzaal Ahmad Zeeshan 17-Dec-16 13:30pm    
Are you using correct endpoints?
Abhijit Parab 17-Dec-16 13:38pm    
It is not a WCF service. If you see my mentioned file paths and code, I have created a class file with attribute scriptservice. In the same class file I have defined WebMethod. I want to call that webmethod from javascript file using jquery Ajax method. But it is not getting called
Ehsan Sajjad 17-Dec-16 15:24pm    
WebMethods are normally static methods, it would not work if it is not made static according to my knowledge, try adding static keyword in method signatures, hope it helps

1 solution

WebMethods are normally static methods, it would not work if it is not made static according to my knowledge, try adding static keyword in method signatures, hope it helps.

For reference you can have a look at this blogpost (Calling a Webmethod Using Jquery Ajax)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900