日期:2014-05-17  浏览次数:20575 次

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&q