简体   繁体   中英

$.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

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.");  
      } 
    });
 });

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>

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>

首先尝试像这样的alert(JSON.stringify(data))对您的json进行字符串化alert(JSON.stringify(data))然后您可能会发现您的错误。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM