Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have developed a service which is running successfully. Following is my service code:

C#
namespace WcfService1
{   
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method="GET", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Wrapped, UriTemplate="/display/{a}/{b}")]        
        string Display(string a, string b);        
    }    
}


My Service:

C#
namespace WcfService1
{
   public class Service1 : IService1
    {
        public string Display(string a, string b)
        {
            int ab = Convert.ToInt32(a);
            int bc = Convert.ToInt32(b);
            int cb = ab + bc;
            return cb.ToString();
        }
    }
}


How do i call this with the help of ajax url? I have tried out the following code but it is not working.

JavaScript
<script type="text/javascript">
        $(document).ready(function () {
            $('#BtnRegister').click(function () {
                debugger;

                var No1 = document.getElementById('TxtFirstNumber').value;
                var No2 = document.getElementById('TxtSecondNumber').value;
                
                $.ajax({
                    cache: false,
                    type: "GET",
                    async: false,
                    url: "http://localhost:22727/Service1.svc/Display",
                    data: 'a=' +No1+'&b='+No2,
                    contentType: "application/json; charset=ytf-8",
                    dataType: "json",
                    processData: true,
                    success: function (result) {
                        alert("data");
                    },
                    error: function (xhr, textStatus, errorThrown) { alert(textStatus + ':' + errorThrown); }
                });
            });
        });
    </script>
Posted

1 solution

you can pass it like that.. :)

JavaScript
data: "{'a':'"+No1+"','b':'"+No2+"'}",


see whole snippet.. :)

JavaScript
$(document).ready(function () {
           $('#BtnRegister').click(function () {
               debugger;

               var No1 = document.getElementById('TxtFirstNumber').value;
               var No2 = document.getElementById('TxtSecondNumber').value;

               $.ajax({
                   cache: false,
                   type: "GET",
                   async: false,
                   url: "http://localhost:22727/Service1.svc/Display",
                   data: "{'a':'"+No1+"','b':'"+No2+"'}",
                   contentType: "application/json; charset=ytf-8",
                   dataType: "json",
                   processData: true,
                   success: function (result) {
                       alert("data");
                   },
                   error: function (xhr, textStatus, errorThrown) { alert(textStatus + ':' + errorThrown); }
               });
           });
       });
 
Share this answer
 
v2
Comments
Nimit Joshi 12-Jun-14 6:56am    
Not Working. Still going to the error line

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