• js 里 用XML httpRequest 调用 Web Service


    用js 调用 web service   今天进行了一下整理 方便以后应用

    要注意web service 的设置 

    //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。   
    [System.Web.Script.Services.ScriptService]    一定要把这句话的默认注释去掉 否则js 无法调用

    Web service 设置
       /// <summary>
        ///判断用户登录的方法
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="userPwd">密码</param>
        /// <returns>成功返回true 否则 false </returns>
        [WebMethod]
        public bool IsUerLogin(string userName,string userPwd)
        {
            //这个 说我自己后台判断 用户名的方法  
            BLL.UserBLL ub = new BLL.UserBLL();
            Model.User us = new Model.User(userName, ub.GetMD5String(userPwd));
    
            string ret = ub.UerLogin(us);
            if (ret != "1")
                return false;
            return true;
        }
    前端 js 代码
    /// <reference path="jquery-1.4.1-vsdoc.js" />
    
    var xmlHttp;
    var controlName;
    // methodName  执行WebService 方法的名称
    //pargm 传递的 参数值
    //c_name 用来显示 返回的文本 的 控件ID
    function GetWebSevice(methodName, pargm, c_name)
    {
    
            var strUrl = "WebService.asmx" + "/" + methodName;
            controlName = c_name;
    
            // 创建 xmlHttpRequest()对象
            try
            {
                    xmlHttp = new window.XMLHttpRequest();
            }
            catch (e)
            {
                    xmlHttp = new window.ActiveXObject("Microsoft.XMLHTTP");
            }
    
            //为 readystate 状态改变 设置回调函数
            xmlHttp.onreadystatechange = state_change;   //调用回调函数
    
            xmlHttp.open("POST", strUrl, false);   //打开
    
            //var  postData= "x=2&y=345"
            //设置发送文件头的信息
            xmlHttp.setRequestHeader("Content-Length", pargm.length);
            xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
    
            //发送参数 
            xmlHttp.send(pargm);
    
            //处理请求的回调函数
            function state_change()
            {
    
    
                    /*
                    readyState  ==4 表示HTTP 请求已经完成
                    status由服务器返回的 HTTP 状态代码,如 200 表示成功,而 
                    404 表示 "Not Found" 错误。
                    当 readyState 小于 3 的时候读取这一属性会导致一个异常。   
                    */
                 
                    if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
                    {
                            document.clear();
                            $("#retControl").attr("innerHTML", xmlHttp.responseText);
    
                    }
            }
    
    
    
    }

    直接调用 GetWebSevice  这个方法传递参数就可以了。

  • 相关阅读:
    putty加了密钥ssh不能登陆,PuTTY:server refused our key问题的解决(转)
    CentOS 7 yum 安装php5.6
    sqlite3.OperationalError: no such table: account_user
    python解决八皇后问题的方法
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 140: invalid continuation byte
    GET /static/plugins/bootstrap/css/bootstrap.css HTTP/1.1" 404 1718
    Java中wait()与notify()理解
    Javac可以编译,Java显示找不到或无法加载主类
    《剑指offer》第三十题:包含min函数的栈
    《剑指offer》第二十九题:顺时针打印矩阵
  • 原文地址:https://www.cnblogs.com/hzy168/p/2981747.html
Copyright © 2020-2023  润新知