Ajax调用后台方法,提示对象未定义,如何解决
前台页面如下:
- C# code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Province.aspx.cs" Inherits="Admin_Channel_Province" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server"> <title>编辑开通省份</title> <link href="../../css/Public.css" rel="stylesheet" type="text/css" /> <script language="javascript" type="text/javascript"> function addch() { if (confirm("您确认要提交所选省份吗?")) { var List = ""; var strID = document.getElementsByName("checkbox1"); for (var i = 0; i < strID.length; i++) { if (strID[i].checked) { List += strID[i].value + ","; } } if (List == "") { alert("请选择省份"); return; } var ch = document.getElementById("ch").value; var ct = document.getElementById("ct").value; Admin_Channel_Province.addprovince(List, ch, ct, GetCallBack); //这里提示Admin_Channel_Province未定义 } function GetCallBack(response) { var result = response.value; if (result == "OK") { alert("编辑成功"); } } } </script></head><body><script src="../../js/Calendar.js" type="text/javascript"></script> <form id="form1" runat="server"> <div class="l_f_top"> <div class="l_f_top_left_div"><img alt="" src="../../images/tab/tb.gif" width="16" height="16" /></div> <div class="l_f_top_right_div">编辑省份</div> </div> <div id="divList" class="l_f_body"> <div align="left"> <input type="checkbox" value="-1" name="allID" id="allID" onclick="javascript:selectAll(this.form,this.checked)" />全选 </div> <input type="checkbox" name="checkbox1" value="1" />福建 <input type="checkbox" name="checkbox1" value="2" checked="checked" />广东 <input type="checkbox" name="checkbox1" value="10" checked="checked" />贵州 <input type="checkbox" name="checkbox1" value="11" />陕西 <input type="checkbox" name="checkbox1" value="12" />青海 <input type="checkbox" name="checkbox1" value="13" />海南 <div class="center l_f_body_foot"> <input type="button" onclick='addch();' value="提 交" class="button_60" /> <input type="button" onclick='javascript:window.history.go(-1);' value="返 回" class="button_60" /> </div> </div> <input type ="hidden" id="ch" name ="ch" value ="<%=CH%>"/> <input type ="hidden" id="ct" name ="ct" value ="<%=CT%>"/> </form></body></html>后台页面如下:
- C# code
using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Text;using t.BLL;using t.DataModel;using t.Common;public partial class Admin_Channel_Province : System.Web.UI.Page{ protected int CH = 0; protected int CT = 0; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { CH = TypeParse.StrToInt32(Request.QueryString["ch"], 0); CT = TypeParse.StrToInt32(Request.QueryString["ct"], 0); } AjaxPro.Utility.RegisterTypeForAjax(typeof(Admin_Channel_Province)); } [AjaxPro.AjaxMethod] public string addprovince(string strID, int CH, int CT) { string str = ""; string err = "OK"; if (strID.IndexOf(",") > 0) { str = strID.Substring(0, strID.Length - 1); } else { str = strID; } try { ProvinceBLL ProvinceBLL = new ProvinceBLL(); if (ProvinceBLL.UpdateProvince(CH, CT, str) < 0) { err = "意外错误,编辑失败!"; } } catch (Exception e) { err = e.Message; } return err; }}
Web.Config的配置如下:
- C# code
<httpHandlers> <!--配置ajaxpro的模块--> <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/> <remove verb="*" path="*.asmx"/> <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/> </httpHandlers>
并且我也引用了AjaxPro.2.dll
编辑环境是Microsoft Visual Studio 2008,在本地调试的时候,前台的JS是可以调用后台的方法的,完全正常。但是传到服务器上以后,提示的错误是Admin_Channel_Province未定义。
请高手帮忙解决。谢谢!!!
[解决办法]
jquery很简单啊,无需服务器端的任何控件
http://dotnet.aspx.cc/file/Return-DataSet-DataTable-Using-jQuery-Ajax-WebMethod.aspx
或者
采用回调的方法
<%@ Page Language="C#" EnableViewState="false" %>
<%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private string ReturnBackValue = " 返回到客户端的内容 | ";
//声明函数,用来返回到客户端。
public string GetCallbackResult()
{
return ReturnBackValue;//将结果返回客户端
}
//声明一个函数,用来接收客户端的参数;函数名字不可以随便,必须是RaiseCallbackEvent才可以
public void RaiseCallbackEvent(String eventArgument)
{
this.ReturnBackValue += eventArgument;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function myfunction(arg) {
<%=Page.ClientScript.GetCallbackEventReference(Page, "arg", "showMsg","")%>;
}
function showMsg(rValue) {
alert(rValue);
}
alert("刷新测试");
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="button" onclick="myfunction('客户端传入的参数')" value="无刷新调用" />
</form>
</body>
</html>
[解决办法]
你看浏览器源代码里面
Admin_Channel_Province的定义有吗
[解决办法]
你这路径
/Manage/ajaxpro/Admin_Channel_OpenProvince,App_Web_z4ozmf3f.ashx
是正确的吗?
[解决办法]
你没有这个文件夹怎么url里面会出现呢?这不是明显的错误吗
[解决办法]
XXX.Admin_Channel_Province.addprovince(List, ch, ct, GetCallBack);
XXX为你项目名称
[解决办法]
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
修改红色的字部分
------解决方案--------------------