Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a webservice"Default.asmx" code behind file in App-Code folder."Default.cs" While button click i want to display Webmethod string from webservice.But not working.How o type url for app_code webservice file?

What I have tried:

webservice
C#
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
//[ScriptService]
public class _Default : System.Web.Services.WebService {

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);

    SqlConnection conRecipes = new SqlConnection(ConfigurationManager.ConnectionStrings["constrRecipes"].ConnectionString);

    public _Default () {

     
         

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    [WebMethod]
    public string AddNominations()
    {
        return "Nomination Added Successfully";
    }

Ajax call
C#
function CallService() {


        $.ajax({
            type: "POST",
            url: "_Default.asmx/AddNominations",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: Success,
            error: Error
        });
    }
    function Success(data, status) {
        $("#lblResult").removeClass("loading");
        $("#lblResult").html(data.d);
    }

    function Error(request, status, error) {
        $("#lblResult").removeClass("loading");
        $("#lblResult").html(request.statusText);
    }
Posted
Updated 5-Apr-17 18:43pm
Comments
Karthik_Mahalingam 5-Apr-17 8:49am    
what is the error message?
GrpSMK 5-Apr-17 9:52am    
Button clicked,no messeges displayed
Karthik_Mahalingam 5-Apr-17 9:54am    
check in chrome console window, if there is any error
GrpSMK 5-Apr-17 9:53am    
when i create webservice ,class file automatically created in app_code folder..whats the use ?how to call AddNominations in ajax call?
ZurdoDev 5-Apr-17 9:57am    
2 things.
1. You have an underscore in the javascript for the file name
2. Put a breakpoint in your success and error functions and see what is happening.

Make the File name uniform

YourWebService.asmx
YourWebService.asmx.cs
url: "YourWebService.asmx/AddNominations",
 
Share this answer
 
v2
function CallService() {

$.ajax
({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "_Default.asmx/AddNominations",
dataType: "json",
success: function (data) {
console.log("success");
},
error: function (x, e) {
console.log("Failure");
}
});


}
Check this it may help you.
 
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