1,diarynew.aspx文件部分代碼:
......
<script language="javascript" type="text/javascript">
<!--function Button_onclick()
{
  alert(WorkNoteService.Xiao());
}
// -->
</script>
......
<form runat="server" enctype="multipart/form-data">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
          <Services>
            <asp:ServiceReference Path="WorkNoteService.asmx" />
          </Services>
        </asp:ScriptManager>
<input id="Button1" style="width: 80px" type="button" value="添加" language="javascript" onclick="Button_onclick()" />
</form>
......2,WorkNoteService.asmx文件部分代码:<%@ WebService Language="C#" Class="WorkNoteService" %>
......
public partial class WorkNoteService : System.Web.Services.WebService 
{
......
[WebMethod]
public string Xiao()
{
  return "Finer";
}
}執行時,提示“undefined”,我要怎样才能得到返回值?谢谢!

解决方案 »

  1.   

    alert(WorkNoteService.Xiao().value);
      

  2.   

    報錯:
    ‘WorkNoteService.Finer().value’是空或不是一个物件
      

  3.   

    我以前也用AJAX类库开发过,都是这样写的,你隐藏类名了嘛?
      

  4.   

    沒有如果不返回值,是可以的。如下面的方法:
    public void AddContent(string xxxStr)
    {
      //写入数据库
    }我用上面的方法是可以寫數據到數據裡的,只是有返回值時不行,得不到值!
      

  5.   

    返回值肯定是这么写的WorkNoteService.Xiao().value,你把所有代码都贴出来看看,要后台的
      

  6.   

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using FXiao.FinerDb.User;
    using FXiao.FinerBusiness;
    using System.Collections;
    using System.Web.Services;
    using System.Web.Script.Services;
    using System.Web.Services.Protocols;
    /// <summary>
    /// Summary description for WorkNoteService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService()]
    public class WorkNoteService : System.Web.Services.WebService 
    {    public WorkNoteService () {        //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }

    string UserID,UserDept,UserFactory;    [WebMethod]
        public string SayHello(String Name) {
            return "Hello : " + Name;
        }

    [WebMethod]
    public int AddData(string attachment)
    {
      int result;
      string connstr=ConfigurationManager.ConnectionStrings["connectionEOffice"].ConnectionString;
      string sqlAdd="insert into wksAttachment(filename) values('"+attachment+"')";
      SqlConnection conn=new SqlConnection(connstr);
      SqlCommand cmd=new SqlCommand(sqlAdd,conn);
      conn.Open();
      result=cmd.ExecuteNonQuery();
      conn.Close();
      
      return result;
    }

    [WebMethod]
    //public  void AddContent(string deptid,string fid,string subject,string content,string workhoures)
    public  void AddContent(string paras)
    {   
      UserDetail();
      
      //UploadFile();
      
      string[] contentList=paras.Split('|');
      string subject,content,workhoures;
      subject="";
      content="";
      workhoures="";
      for(int i=0;i<contentList.Length-1;i++)
      {
        subject=contentList[0];
    content=contentList[1];
    workhoures=contentList[2];
      }
      
      SqlParameter[] parameters=
      {
      new SqlParameter("@user_id",SqlDbType.VarChar),
      new SqlParameter("@dept_id",SqlDbType.VarChar),
      new SqlParameter("@f_id",SqlDbType.VarChar),
      new SqlParameter("@subject",SqlDbType.NVarChar,200),
      new SqlParameter("@content",SqlDbType.NVarChar,3000),
      new SqlParameter("@workhoures",SqlDbType.VarChar),
      };
      parameters[0].Value=User.Identity.Name;
      parameters[1].Value=UserDept;
      parameters[2].Value=UserFactory;
      parameters[3].Value=subject;
      parameters[4].Value=content;
      parameters[5].Value=workhoures;
      
      int rows=0;
      int result;
      Diary myDiary=new Diary("server=10.142.60.169;database=ud_eoffice;uid=xxxx;pwd=yyyy");
      myDiary.Add("wks_DataAdd","StoredProcedure",parameters,rows);
      /*Response.Write(@"<script>parent.bottomFrame.location.href='diaryshow.aspx'</script>");*/
    }  

    void UserDetail()
    {
      string sqlstr="";
      sqlstr=sqlstr+" select p003_02,p003_03,p003_05,p003_07,p002_02,p002_03 from ut_p003 t";
      sqlstr=sqlstr+" join ut_p002 a ";
      sqlstr=sqlstr+" on a.p002_02=t.p003_07 ";
      sqlstr=sqlstr+" where p003_02='"+User.Identity.Name+"'";
      
      FUser myUser=new FUser("server=10.142.60.169;database=ud_paper;uid=acmesql;pwd=usig");
      SqlDataReader dr=myUser.GetUserDetail(sqlstr);

      while(dr.Read())
      {
    UserDept=dr["p003_07"].ToString();
    UserFactory=dr["p003_05"].ToString();
      }


    [WebMethod]
    public void UploadFile()
    {
      DateTime nowTime=DateTime.Now;
      string savePath=Server.MapPath("\\Attachments\\");
      string filename="";
      //string fileExt="";
      HttpFileCollection files = HttpContext.Current.Request.Files;
          for(int i=0;i<files.Count;i++)
      {
        HttpPostedFile postedFile=files[i];
    filename=nowTime.ToString("yyyymmddhhss")+System.IO.Path.GetFileName(postedFile.FileName);
    //fileExt=System.IO.Path.GetExtension(postedFile.FileName);
    postedFile.SaveAs(savePath+filename);
      }
    }

    [System.Web.Services.WebMethod]
    //[System.Web.Script.Services.ScriptMethod]
    public string Xiao(string name)
    {
      return string.Format("Hello {0}!", name);
    }
    }
    以上就是全部代码。
    後面這個方法Xiao
    前面的AddContent可以
    非常感谢!
      

  7.   

    我按這個教程也不行,真是奇怪。
    就是不能得到返回值!http://www.cnblogs.com/dflying/archive/2007/06/05/771490.html
      

  8.   

    你仔细看AJAX的Sample会发现:在调用HelloWorld(string query)的时候 使用的是 WebService.HelloWorld(parm,OnRequestComplete);接着有function OnRequestComplete(result){}函数
    问题就在这个地方
    所以你的代码要修改成
    function showAlert()
    {
    WorkNoteService.Xiao(setValue);
    }function setValue(s)
    {
        alert(s);
    }
    欢迎访问我的BLOG,里面有个原创文章综合讲了ajax调Webservice出现错误的
      

  9.   

    ajax调Webservice没研究,不好意思帮不了你了。
      

  10.   

    要写回调函数同意appgqp(半个苹果)
      

  11.   

    to appgqp(半个苹果)那個方法我用過,他會提示:undefined
      

  12.   

    to  appgqp(半个苹果)你的方法应该是这样吧:function showAlert()
    {
       WorkNoteService.Xiao(paras,setValue);
    }
      

  13.   

    如果WebService.Methord(parm1,……)
    对应的JAVASCIPT是
    function showAlert()
    {
    WebService.Methord(parm1,……,setValue);
    }
    function setValue(s)
    {
        alert(s);
    }但是WebService.Methord没有传入参数怎么写我也已经印象不深了,要不你给WebService添加一个没有用的参数^_^
    或者你查看下源文件,看看"<script src=.axd?d=CSS1Q54nXPsZkV4CgkofReExwY2TTFI0QayaxCmC6WGKSNyF6g6KiugHG5NlLeI6Qs7pxpF86pW8Wy-wi0En6iF5bDexCfge5J2vK-hSjwE1&amp;t=633174158700000000" pe="text/javascript"></script>" 类似的那一串是什么样子的,再根据具体情况改进一下
      

  14.   

    所以你的代码要修改成
    function showAlert()
    {                            //下面的函数
    WorkNoteService.Xiao(setValue,setValue);
    }function setValue(s)
    {
        alert(s);
    }
      

  15.   

    to tianzhi21(花无第二阶段)这个我试过,一样是提示:undefined
      

  16.   

    你在测试的出现了"undifine" 以后你看看浏览器的的 左下角,看看报的是什么错也可能是Web.config文件的引用与实际不符合,来统一版本
      

  17.   

    to appgqp(半个苹果) 這時沒有報錯。傳回的值就是undefined是不是服务器端与client端的类型不一致?用下面的代碼就返回[object]function GetResult(result)
    {
      alert(result);
    }
      

  18.   

    成功。方法如上面各位高手所说,只是我总在調用js函數時加上"()"错误代码:
    function showAlert()
    {
       WorkNoteService.Xiao(paras,setValue());//我这里加上了()
    }正确代码:
    function showAlert()
    {
       WorkNoteService.Xiao(paras,setValue);
    }
    谢谢大家!
      

  19.   

    请各位高手再看看这个帖子:http://community.csdn.net/Expert/topic/5758/5758768.xml?temp=.5194971谢谢!
      

  20.   

    要用一个回调函数来接受返回值的,,如果能直接.value就不叫ajax了