【问题标题】:c# Jquery succes function not called after calling Web Servicec#调用Web服务后不调用jquery成功函数
【发布时间】:2015-02-08 21:04:45
【问题描述】:

我尝试执行我想在我的网站中使用的代码。我从《MCTS 自定进度培训套件(考试 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 调用调用 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 是返回员工属性的非常简单的代码。它正确填写了属性。我可以在 Visual Studio 的调试器中查看。但是,当光标返回到 Default.aspx 中的 ajax-call 时,不会调用 return-function。错误函数都没有被调用。我试图用 Chrome Devtools 调试它,但我唯一能看到它什么也没返回。它不会导致错误。选中“捕获异常时暂停”。

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

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

罗伊

工作环境雇主: 视觉工作室 2010 互联网浏览器 8.0.6001.18702 谷歌浏览器 39.0.2171.95 m Windows Server 2003 不调用成功不报错(我的工作环境)

家居环境: 视觉工作室 2010 Windows 7的 互联网浏览器 11 (不调用成功,但调用错误(我的私人环境))

【问题讨论】:

  • ajax函数中success和cache的拼写错误

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


【解决方案1】:
   $.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.");
        }

    });

【讨论】:

  • 天啊...这真的很糟糕。我期待编译器会出错。但不是。所以就这么简单....谢谢!现在我可以依赖 VS(但不再依赖我自己了)
猜你喜欢
  • 2021-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多