Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am trying to call a method from web service(.asmx) present in same solutions itself(I am using Visual Studio 2012 Web Express) using Jquery Ajax Calls, the control hitting the method and getting back to jquery call. Please tell what changes I need to make In web configue or in any place.
JavaScript
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
 <script type="text/javascript">
        $(document).ready(function () {
                  $("#btnUploadFile").click(function (e) {
                debugger;
                var filename=$("#<%=flUpload.ClientID%>").val();
              }
                $.ajax({
                    type: 'POST',
                    //data: "{'FileName': "+ filename +"}",
                    url: "UploadEmployee.asmx/Test",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        alert(msg);
                    }
                });
            });
        });
    </script>


C#
using Sonata.Finance.BusinessObjects;
using Sonata.Finance.Services;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;

namespace FinanceWebApplication.WebServices
{
    /// <summary>
    /// Summary description for UploadEmployee
    /// </summary>
    [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]
    public class UploadEmployee : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string Test()
        {
            return "Hi";
        }
    }


}
Posted
Updated 31-Jul-14 5:37am
v2
Comments
Suresh Suthar 31-Jul-14 5:04am    
Just read the documentation on the jQuery website. I think you need GET in the AJAX call.
santoshkumar413 31-Jul-14 5:11am    
Hi,
I kept GET, but not working. Getting same problem,

I have modified and added on error with alert, I am getting alert error message as 500 Internal Server Error

$(document).ready(function () {
$("#btnUploadFile").click(function (e) { $.ajax({
type: 'POST',
url: "UploadEmployee.asmx/Test",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg);
}
,
error: function (response) {
alert(response.status + " " + response.statusText);
}
});
});
});
Suresh Suthar 31-Jul-14 5:31am    
Then debug your webservice you VS.
santoshkumar413 31-Jul-14 5:39am    
Hi, Thanks for the reply...... if I debugg service its working fine, but when I call the service i am getting 500 Internal Server Error
santoshkumar413 31-Jul-14 6:00am    
Hi,
Thanks for Reply I found solution I have added resolve url javascript method
var pageUrl = '<%=ResolveUrl("~/WebServices/UploadEmployee.asmx")%>', which is hitting my service.
Then the final Script is


$(document).ready(function () {
$("#btnUploadFile").click(function (e) {
var pageUrl = '<%=ResolveUrl("~/WebServices/UploadEmployee.asmx")%>'
$.ajax({
type: 'POST',
url: pageUrl + "/Test",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg);
}
,
error: function (response) {
alert(response.status + " " + response.statusText);
}
});
});
});

1 solution

Simply create webmethod in your codebehind file
C#
[WebMethod]
public string Test()
{
     return "Hi";
}

that webmethod in ajax.
$.ajax({
              type: 'POST',
             //data: "{'FileName': "+ filename +"}",
              url: "yourpage.aspx/Test",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (msg) {
                    alert(msg);
                  }
             });
 
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