日期:2014-05-16  浏览次数:20725 次

javascript 调用websevice 内的方法不到 是怎么回事? - Web 开发 / Ajax
webservice代码:
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.BLL;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() 
    {
        
        return "Hello World";
    }
    [WebMethod]
    public string UserLogin(string username,string userpwd,string yzm)
    {
        if (new Maticsoft.BLL.UserInfo().Exists(username))
        {
            DataTable table = new Maticsoft.BLL.UserInfo().GetList(" (UserName='" + username + "' or UserMaill='" + username + "') and UserPwd='" + userpwd + "' ").Tables[0];
            if (table.Rows.Count > 0)
            {
                return "OK";
            }
            else
            {
                return "NO";
            }

        }
        else
        {
            return "用户名不存在";
        }
        
      
    }
    
}


javascript代码
  function CheckUser_login() {
  document.getElementById("login").style.display="none";
   
  document.getElementById("userlogin").innerHTML = "<img src='/web/img/1322185.gif' />";
  http.Open("POST", "WebService.asmx/UserLogin", true);

  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  http.send("username=" + document.getElementById("username").value + "&userpwd=" + document.getElementById("userpwd").value + "&yzm=" + document.getElementById("yzm").value);

   
  http.onreadystatechange = CheckUserlogin_GetResult;
   
  }

------解决方案--------------------
ajax发送的数据需要构造成soap信封的格式~

直接发送键值对无效的~

参考这篇文章
Ajax调用WebService