首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Web成功调用c#服务后未调用的函数

Web成功调用c#服务后未调用的函数
EN

Stack Overflow用户
提问于 2014-12-10 23:36:21
回答 1查看 476关注 0票数 0

我尝试执行这段代码,我想在我的网站上使用。我从"MCTS自定进度训练工具包(考试70-515)“一书中抄袭过来的。

这是Default.aspx:

代码语言:javascript
复制
<%@ 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:

代码语言:javascript
复制
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属性的非常简单的代码。它正确地填写了属性。我可以在Visual Studio的调试器中查看它。但是,当光标返回到Default.aspx中的ajax-call时,不会调用返回函数。这两个错误函数都没有被调用。我试着用Chrome Devtools调试它,但我唯一能看到的就是什么也没有返回。它不会导致错误。选中“在捕获的异常时暂停”。

在Internet Explorer中,“活动脚本”处于启用状态。JQuery无需调用服务即可正常工作。我已经通过StackOverflow尝试了与这个问题相关的解决方案。然而,没有成功。

我已经花了几天的时间来解决这个问题。请帮帮忙。

罗伊

工作环境雇主: Visual Studio 2010 Internet Explorer 8.0.6001.18702 Google Chrome 39.0.2171.95 m Windows Server 2003不调用成功或错误(我的工作环境)

家庭环境: Visual Studio 2010 Windows 7 Internet Explorer 11 (不调用succes,但调用error (我的私有环境))

EN

回答 1

Stack Overflow用户

发布于 2014-12-11 00:38:58

代码语言:javascript
复制
   $.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.");
        }

    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27404917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档