0

我正在尝试通过 jQuery 调用 web 服务,但它没有显示任何结果或错误。我的代码是:

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

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

出了什么问题希望得到你的建议

谢谢

4

4 回答 4

2

你可以把它放在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.");  
      } 
    });
 });
于 2013-02-06T12:21:11.447 回答
1

dataType 以 json 形式给出。这意味着 jquery 将从服务器收到的响应解析为 json。但是您在服务中返回字符串。所以 jQuery 会抛出解析错误。

尝试附加complete(jqXHR jqXHR, String textStatus)回调。并查看 textStatus

尝试使用

<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>
于 2013-02-06T12:21:28.913 回答
0

尝试这个:

<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>
于 2013-02-06T12:36:45.613 回答
0

首先尝试像这样对你的json进行字符串化, alert(JSON.stringify(data))然后也许你可以找到你的错误..

于 2013-02-06T12:37:15.113 回答