调用Web Service后未调用c#Jquery成功函数

时间:2014-12-10 15:36:21

标签: jquery ajax web-services c#-4.0 asp.net-ajax

我尝试执行此代码,我想在我的网站中使用。我从“MCTS Self-Paced Training Kit(考试70-515)”一书中复制了它。

这是Default.aspx:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplicationTestjQuery._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <script type="text/javascript">

    $(document).ready(function () {
        $("#MainContent_ButtonSearch").click(function () {
            // hide employee details
            $("empDetails").hide("slow");

            var empId = $("#MainContent_TextBoxEmpId").val();

            $.ajax({
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                url: "EmployeeService.asmx/GetEmployeeById",
                data: "{'employeeId': '" + empId.toString() + "'}",
                cashe: false,
                succes: function (data) {
                    $("#textId").html(data.d.ID);
                    $("#textName").html(data.d.FullName);
                    $("#textTitle").html(data.d.Title);
                    $("#textDepartment").html(data.d.Department);

                },
                error: function () {
                    alert("Error calling the webservice.");
                }

            });
            $("#empDetails").show("slow");
        });
    });
</script>

<h2>Employee Lookup</h2>
<hr />
Enter Employee Id
<br />
    <asp:TextBox ID="TextBoxEmpId" runat="server"></asp:TextBox>&nbsp;
    <br />
    <input id="ButtonSearch" type="button" value="Search" runat="server" />

    <div id="empDetails" style="display: none;margin-top: 40px"> 
        <h2>Employee Details</h2>
        <hr />
        <b>ID:</b>&nbsp;<span id="textId"></span><br />
        <b>Name:</b>&nbsp;<span id="textName"></span><br />
        <b>Title:</b>&nbsp;<span id="textTitle"></span><br />
        <b>Department:</b>&nbsp;<span id="textDepartment"></span><br />
    </div>
</asp:Content>

ajax-call调用EmployeeService:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;


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

        [WebMethod]
        public Employee GetEmployeeById(string employeeId)
        {
            //simulate name-lookup
            return new Employee(employeeId);

        }

    }

    [Serializable]
    public class Employee
    {
        public Employee(string empId)
        {

            //simulate lookup employee
            this.ID = empId;
            this.FullName = "Roy C.";
            this.Title = "Webdeveloper";
            this.Department = "CBS";
        }

        public Employee() { }

        public string FullName { get; set; }
        public string ID { get; set; }
        public string Title { get; set; }
        public string Department { get; set; }
    }
}

如您所见,EmployeeService是返回employee-properties的非常简单的代码。它填写了正确的属性。我可以在Visual Studio的调试器中查看。但是,当游标返回Default.aspx中的ajax-call时,不会调用返回函数。没有调用错误函数。我尝试使用Chrome Devtools进行调试,但我唯一能看到的就是什么都没有。它不会导致错误。选中“暂停捕获的异常”。

在Internet Explorer中启用“活动脚本”。 JQuery在不调用服务的情况下运行良好。我已经通过StackOverflow尝试了与此问题相关的解决方案。但是没有成功。

我已经花了好几天才解决这个问题。请帮忙。

罗伊

工作环境雇主: Visual Studio 2010 Internet Explorer 8.0.6001.18702 谷歌浏览器39.0.2171.95米 Windows Server 2003不会调用成功或错误(我的工作环境)

家居环境: Visual Studio 2010 Windows 7的 Internet Explorer 11 (不会调用成功,但会调用错误(我的私人环境))

1 个答案:

答案 0 :(得分:0)

   $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json",
        url: "EmployeeService.asmx/GetEmployeeById",
        data: "{'employeeId': '" + empId.toString() + "'}",
        cache: false,
        success: function (data) {
            $("#textId").html(data.d.ID);
            $("#textName").html(data.d.FullName);
            $("#textTitle").html(data.d.Title);
            $("#textDepartment").html(data.d.Department);

        },
        error: function () {
            alert("Error calling the webservice.");
        }

    });