Syed Raza
Syed Raza

Reputation: 331

$.ajax not calling WebService in ASP.Net

I am trying to call webservice through jQuery but it is not showing any results or errors. My code is:

<script type="text/javascript" language="javascript">
    var empId = '<%= Session["UserName"] %>';
</script>
<script type="text/javascript"> 
    alert(empId);    
    $.ajax({ 
        type: "POST", 
        dataType: "json", 
        contentType: "application/json", 
        url: "ServiceGetUser.asmx/GetEmployee", 
        data: "{'employeeId': '" + empId + "'}", 
        success: function (data) { 
            alert("Employee name: " + data.d); 
        }, 
        error: function () { 
            alert("Error calling the web service.");  
        } 
    }); 
</script>

I'm getting a value from session printing it successfully then passing it to the webservice and in return printing name Jane Developer as shown in my webservice code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace MentorMentee
{
    /// <summary>
    /// Summary description for ServiceGetUser
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [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 ServiceGetUser : System.Web.Services.WebService
    {

        [WebMethod]
        public string GetEmployee(string employeeId)
        {
            return "Jane Developer";
        }
    }
}

what's wrong is going on hopes for your suggestions

Thanks

Upvotes: 0

Views: 1719

Answers (4)

Sedat Başar
Sedat Başar

Reputation: 3798

First try to stringify your json like this alert(JSON.stringify(data)) then maybe u can find your mistake..

Upvotes: 0

coder
coder

Reputation: 13248

Try this:

<script type="text/javascript" language="javascript">
    var empId = '<%= Session["UserName"] %>';
</script>
<script type="text/javascript"> 
    alert(empId);    
    $.ajax({ 
        type: "POST", 
        dataType: "json", 
        contentType: "application/json; charset=utf-8", 
        url: "ServiceGetUser.asmx/GetEmployee", 
        data: '{employeeId: "' + $("#<%=employeeId.ClientID%>")[0].value + '" }',
        success: function (data) { 
            alert("Employee name: " + data.d); 
        }, 
        error: function () { 
            alert("Error calling the web service.");  
        } 
    }); 
</script>

Upvotes: 0

Jai
Jai

Reputation: 74738

You can put this in doc ready block:

$(window).load(function(){
   $.ajax({ 
      type: "POST", 
      dataType: "json", 
      contentType: "application/json", 
      url: "ServiceGetUser.asmx/GetEmployee", 
      data: {'employeeId': '<%= Session["UserName"] %>'}, //<-- try it
      success: function (data) { 
          alert("Employee name: " + data.d); 
      }, 
      error: function () { 
          alert("Error calling the web service.");  
      } 
    });
 });

Upvotes: 2

Selvaraj M A
Selvaraj M A

Reputation: 3136

dataType is given as json. Which means that jquery will parse the response received from server into json. But you are returning string in your service. So parse error will be thrown by jQuery.

Try attaching complete(jqXHR jqXHR, String textStatus) callback. And look at textStatus

Try using

<script type="text/javascript" language="javascript">
    var empId = '<%= Session["UserName"] %>';
</script>
<script type="text/javascript"> 
    alert(empId);    
    $.ajax({ 
        type: "POST", 
        dataType: "json", 
        contentType: "application/json", 
        url: "ServiceGetUser.asmx/GetEmployee", 
        data: "{'employeeId': '" + empId + "'}", 
        success: function (data) { 
            alert("Employee name: " + data.d); 
        }, 
        error: function () { 
            alert("Error calling the web service.");  
        } ,
        complete: function(xhr, textStatus) {
            alert(textStatus);
        }
    }); 
</script>

Upvotes: 1

Related Questions