【问题标题】:Function not defined error while using asp.net ajax使用 asp.net ajax 时函数未定义错误
【发布时间】:2015-07-16 23:00:47
【问题描述】:

我正在尝试通过以下代码通过 asp.net ajax 调用 Web 服务

namespace MCTS70515AJAX
{
public static class HR
{
    public static int GetEmployeeCount(string department)
    {
        int count = 0;
        switch (department)
        {
            case "Sales":
                count = 10;
                break;
            case "Engineering":
                count = 28;
                break;
            case "Marketing":
                count = 44;
                break;
            case "HR":
                count = 7;
                break;
            default:
                break;
        }
        return count;
    }
}

这是我正在渲染的 aspx 页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AJAX2.aspx.cs" 

Inherits="MCTS70515AJAX.AJAX2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <title></title>

</head>
<body>

    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="HRSer.asmx" />
            </Services>
            <Scripts>

            </Scripts>
        </asp:ScriptManager>


        <div>
            <select id="Departments" size="5">
                <option value="Engineering">Engineering</option>
                <option value="HR">Human Resources</option>
                <option value="Sales">Sales</option>
                <option value="Marketing">Marketing</option>

            </select>

        </div>
        <br />
        <div>
            <span id="employeeResults"></span>
            <span id="loading" style="display: none;">&nbsp;&nbsp;Loading ... 

            </span>

        </div>

    </form>
     <script type="text/javascript">

    var departments = null;
    Sys.Application.add_load(page_load);
    Sys.Application.add_unload(page_unload);
    function page_load(sender, e) {
        departments = $get("Departments");
        $addHandler(departments, "change", departments_onchange);
    }
    function page_unload(sender, e) {
        $removeHandler(departments, "change", departments_onchange);
    }
    function departments_onchange(sender, e) {
        $get("employeeResults").innerHTML = ""; $get("loading").style.display = "block";
        var selectedValue = departments.value;
        HRService.Getcount(selectedValue, onSuccess);
    }
    function onSuccess(result) {
        $get("loading").style.display = "none";
        $get("employeeResults").innerHTML = "Employee count: " + result;
    }


                </script>

</body>
</html>

这是我正在调用的网络服务

namespace MCTS70515AJAX
{

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class HRService : System.Web.Services.WebService
{
    [ScriptMethod]
    [WebMethod]
    public int Getcount(string department)
    {
        return HR.GetEmployeeCount(department);
    }
}

} 页面呈现正常,但每当我更改列表项值时,它都会显示 JavaScript 运行时错误:“HRService”未定义。这是为什么。

对不起,这么长的帖子....

【问题讨论】:

    标签: javascript c# asp.net ajax asp.net-ajax


    【解决方案1】:

    您可以尝试PageMethods,只需添加using[WebMethod]

    using System.Web.Services;
    
    public static class HR
        {
            [WebMethod]
            public static int GetEmployeeCount(string department)
            {
                int count = 0;
                ...
                return count;
            }
        }
    

    在你的 aspx 中像这样修改你的 scriptManager

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    

    那么你可以这样调用JS中的方法

    function myJS_method() {
        var departments = $get("Departments"); // your string value
        PageMethods.GetEmployeeCount(departments , onSucess, onError);
        function onSucess(result) {
            // your code when it is OK
            // result is the return value of your C# method
        }
        function onError(result) { alert('Error' + result); }
    }
    

    【讨论】:

    • 这不起作用,现在它说 JavaScript 运行时错误:'PageMethods' is undefined
    • 您确定在ScriptManager中添加EnablePageMethods属性,在方法中添加[WebMethod]并添加使用行吗?这最后两件事在您的 HR 课程中而不是在您的 HRService 中
    • 这确实有效,非常感谢,但你能告诉在使用网络服务时如何使用它(.asmx)
    • @Vishesh 使用 WCF 服务,而不是 asmx。如果您有现有的 asmx 服务,您可以使用说明here 进行迁移
    猜你喜欢
    • 2010-11-05
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多