简体   繁体   English

$ .ajax不调用ASP.Net中的WebService

[英]$.ajax not calling WebService in ASP.Net

I am trying to call webservice through jQuery but it is not showing any results or errors. 我正在尝试通过jQuery调用网络服务,但未显示任何结果或错误。 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: 我从session成功打印中获取了一个值,然后将其传递到Web服务,并返回打印名称Jane Developer ,如我的Web服务代码所示:

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 : 您可以将其放在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. dataType作为json给出。 Which means that jquery will parse the response received from server into json. 这意味着jquery将将从服务器接收到的响应解析为json。 But you are returning string in your service. 但是您在服务中返回字符串。 So parse error will be thrown by jQuery. 因此解析错误将由jQuery引发。

Try attaching complete(jqXHR jqXHR, String textStatus) callback. 尝试附加complete(jqXHR jqXHR, String textStatus)回调。 And look at textStatus 并查看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))然后您可能会发现您的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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