使用asp.net ajax时函数未定义错误 [英] Function not defined error while using asp.net ajax

查看:26
本文介绍了使用asp.net ajax时函数未定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

命名空间 MCTS70515AJAX{公共静态类HR{public static int GetEmployeeCount(字符串部门){整数计数 = 0;切换(部门){案例销售":计数 = 10;休息;案例工程":计数 = 28;休息;案例营销":计数 = 44;休息;案例人力资源":计数 = 7;休息;默认:休息;}返回计数;}}

这是我渲染的aspx页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AJAX2.aspx.cs"继承="MCTS70515AJAX.AJAX2" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="服务器"><title></title><身体><form id="form1" runat="server"><asp:ScriptManager ID="ScriptManager1" runat="server"><服务><asp:ServiceReference Path="HRSer.asmx"/></服务><脚本></脚本></asp:ScriptManager><div><select id="Departments" size="5"><option value="Engineering">Engineering</option><option value="HR">人力资源</option><option value="Sales">Sales</option><option value="Marketing">Marketing</option></选择>

<br/><div><span id="employeeResults"></span><span id="loading" style="display: none;">&nbsp;&nbsp;正在加载...</span>

</表单><script type="text/javascript">var 部门 = null;Sys.Application.add_load(page_load);Sys.Application.add_unload(page_unload);函数 page_load(sender, e) {部门 = $get("部门");$addHandler(部门,改变",部门_onchange);}函数 page_unload(sender, e) {$removeHandler(部门,改变",部门_onchange);}功能部门_onchange(发件人,e){$get("employeeResults").innerHTML = "";$get("loading").style.display = "block";var selectedValue = 部门.价值;HRService.Getcount(selectedValue, onSuccess);}功能成功(结果){$get("loading").style.display = "none";$get("employeeResults").innerHTML = "员工人数:" + result;}

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

命名空间 MCTS70515AJAX{[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.ComponentModel.ToolboxItem(false)][System.Web.Script.Services.ScriptService]公共类 HRService : System.Web.Services.WebService{[脚本方法][网络方法]public int Getcount(字符串部门){返回 HR.GetEmployeeCount(部门);}}

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

抱歉发了这么长的帖子....

解决方案

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

使用 System.Web.Services;公共静态类HR{[网络方法]public static int GetEmployeeCount(字符串部门){整数计数 = 0;...返回计数;}}

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

</asp:ScriptManager>

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

function myJS_method() {var 部门 = $get("部门");//你的字符串值PageMethods.GetEmployeeCount(departments , onSucess, onError);功能 onSucess(结果){//当你的代码没问题时//结果是 C# 方法的返回值}函数 onError(result) { alert('Error' + result);}}

i am trying to call a web service through asp.net ajax by the following code

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

this is the aspx page i am rendering

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

this is the web service i am calling

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

} the page renders fine but whenever i change the list item value, it shows JavaScript runtime error: 'HRService' is undefined. why is this.

Sorry for such a long post ....

解决方案

You can can try PageMethods, just add the using and the [WebMethod]

using System.Web.Services;

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

In your aspx modify your scriptManager like this

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

then you can call the method in the JS this way

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

这篇关于使用asp.net ajax时函数未定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆