首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

js调用webservices报错webservices未定义 解答

2012-08-15 
js调用webservices报错webservices未定义 在线等解答C# codeusing Systemusing System.Collectionsusing

js调用webservices报错webservices未定义 在线等解答

C# code
using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Linq;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.Xml.Linq;using System.Web.Script.Services;using System.Collections.Generic;namespace WebApplication1.App_Code{    /// <summary>    /// WebService1 的摘要说明    /// </summary>    [WebService(Namespace = "http://www.hodoja.com/")]    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。     [System.Web.Script.Services.ScriptService]    public class WebService1 : System.Web.Services.WebService    {        public WebService1()        {         }        [WebMethod(EnableSession=true)]        public List<Model.UserInfo> GetAll()        {            return new BLL.UserManager().GetAll();        }        [WebMethod(EnableSession = true)]        public Model.UserInfo LoginUser(string uname, string upwd)        {            return new BLL.UserManager().LoginUser(uname,upwd);        }    }}


JScript code
function LoginUser(){    var uname = document.getElementById("txtUname").value;    var upwd = document.getElementById("txtUpwd").value;        WebService1.LoginUser(uname,upwd,WebServiceSucceededMethod,WebServiceFailedMethod,LoginUserCallBack)    }function LoginUserCallBack(result){    if(result !=null)    {        alert("成功");    }    else    alert("失败");}//WebService调用成功时回调的函数function WebServiceSucceededMethod(result, callback){    try{        if (callback != null)            callback(result);    }    catch(err){        if (top.debug)        {            alert('错误:'+err.message)        }    }}//WebService调用失败时回调的函数function WebServiceFailedMethod(result, callback){    try{        if (callback != null)        {            callback(result);        }        else        {            alert('网络错误,请稍候再试!');        }    }    catch(err){        if (top.debug)        {            alert('错误:'+err.message)        }        else        {            alert('网络错误,请稍候再试!');        }    }}

页面就2个textbox和一个按钮 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!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 runat="server">
  <title>登陆页面</title>

   
   
</head>
<body>
   
 
  <form id="form1" runat="server">
  <asp:ScriptManager ID="ScriptManager1" runat="server">
  <Scripts>
  <asp:ScriptReference Path="~/Main.js"/>
  </Scripts>
  <Services>
  <asp:ServiceReference Path="~/App_Code/WebService1.asmx"/>
  </Services>
  </asp:ScriptManager>
  <div>
  用户名<asp:TextBox ID="txtUname" runat="server"></asp:TextBox><br />
   
   
  密码 <asp:TextBox ID="txtUpwd" runat="server" TextMode="Password"></asp:TextBox><br />
   
  <asp:Button runat="server" ID="btnLogin" Text="登陆" OnClientClick="LoginUser();"/>


   
  </div>
  </form>
</body>
</html>




[解决办法]
你可以在页面放一个javascriptmanager控件,使用ajax框架代码实现
后台CS文件
/// <summary>
/// 检查用户名是否占用
/// </summary>
/// <param name="userName">输入的附件名</param>
/// <returns>检查结果,为“ok"或出错信息</returns>
[System.Web.Services.WebMethod]
public static string IsFileNameExist(string userName)
{
System.Threading.Thread.Sleep(3000);

if (BLL.UsersBLL.IsValidataUserName(userName) == false)
{
return "ok";
}
else
{
return "对不起,你输入的用户名" + userName + "已被占用,请输入其他名字后再试";
}
}

 js 文件里用
PageMethods.IsFileNameExist(document.getElementById(sid).value, DealWithResult);

 方法调用后台代码
[解决办法]
WebService1.LoginUser(uname,upwd,WebServiceSucceededMethod,WebServiceFailedMethod,LoginUserCallBack) 改成:
WebApplication1.App_Code.WebService1.LoginUser(uname,upwd,WebServiceSucceededMethod,WebServiceFailedMethod,LoginUserCallBack)......即加上命名空间....

[解决办法]
你webservice返回的数据类型,我看好像也不对,要序列化,返回自己定义的类
http://www.cnblogs.com/lxinxuan/archive/2007/05/24/758317.html
这个你看看

热点排行