专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Web Service

使用ajax调用webservice,为啥参数没有传进来,可是方法已经找到

发布时间:2010-05-20 14:01:29 文章来源:www.iduyao.cn 采编人员:星星草
使用ajax调用webservice,为什么参数没有传进来,可是方法已经找到
JS 脚本如下:
<script type="text/javascript">
        function webservice(url, options) {
            if (typeof (url) == 'object') { //将url写在options里的情况
                options = url;
                url = url.url;
            }
            if (!url) return;
            if (options.dataType.toLowerCase() == 'json') { //请求JSON格式的数据时,url后面需要加上“/方法名”
                url = url + '/' + options.method;
            }
            var xmlHttp = getXmlHttp(); //获取XMLHttpRequest对象
            xmlHttp.open('POST', url, true); //异步请求数据
            xmlHttp.onreadystatechange = function () {
                if (xmlHttp.readyState == 4) {
                    try {
                        if (xmlHttp.status == 200 && typeof (options.success) == 'function') {
                            options.success(xmlHttp.responseText);
                        }
                        else if ((xmlHttp.status / 100 == 4 || xmlHttp.status / 100 == 5) && typeof (options.error) == 'function') {
                            options.error(xmlHttp.responseText, xmlHttp.status);
                        }
                        else if (xmlHttp.status / 100 == 200 && typeof (options.complete) == 'function') {
                            options.complete(xmlHttp.responseText, xmlHttp.status);
                        }
                        else if (typeof (options.failed) == 'function') {
                            options.failed(xmlHttp.responseText, xmlHttp.status);
                        }
                    }
                    catch (e) {
                    }
                }
            }
            xmlHttp.setRequestHeader('Content-Type', options.contentType); //设置请求头的ContentType
            xmlHttp.setRequestHeader('SOAPAction', options.namespace + options.method); //设置SOAPAction
            xmlHttp.send(options.data); //发送参数数据
        }
        function getXmlHttp() {
            var xmlHttp;
            if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlHttp = new XMLHttpRequest();
            }
            else { // code for IE6, IE5
                xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
            }
            return xmlHttp;
        }
        function testPostWebService() {
            var data = '<?xml version="1.0" encoding="utf-8"?>'
                + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
                    + '<soap:Body>'
                        + '<MsgSendReq>'
                            + '<UserName>Krime</UserName><Password>123456</Password><IDCard>123456789145326</IDCard><Moblie>15863254789</Moblie><RealName></RealName><Email>qq@qq.com</Email><VerifyInfo>fsdfsd</VerifyInfo>'
                        + '</MsgSendReq>'
                    + '</soap:Body>'
                + '</soap:Envelope>';
            var options = {
                namespace: 'http://tempuri.org/',
                method: 'RegUser',
                contentType: 'text/xml; charset=utf-8',
                dataType: 'xml',
                data: data,
                success: function (msg) {
                    alert(msg);
                }
            };
            webservice('http://localhost:7501/RegisterService.asmx', options);
        };

    </script>
webservice方法如下:
 /// 用户注册
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    [WebMethod(Description = "用户注册")]
    public string RegUser(string xml)
    {
        if (string.IsNullOrEmpty(xml))
        {
            return "<MsgResp><ResultCode>3</ResultCode><ResultMsg>传入数据为空</ResultMsg></MsgResp>";
        }
        QHSCReg.MsgSendReq reqEntity = getReqFromXml(xml);
        return RegUserInfo(reqEntity.UserName, reqEntity.Password, reqEntity.IDCard, reqEntity.Moblie, reqEntity.RealName, reqEntity.Email, reqEntity.VerifyInfo);
    }
现在永远只得到传入数据为空的输出,请问为什么,谢谢
------解决思路----------------------
换种写法试试~ ajax调用webservice有多种写法
------解决思路----------------------
data: data
不用关键字不好吗,你取的变量名跟关键字一样了
------解决思路----------------------
引用:
Quote: 引用:

换种写法试试~ ajax调用webservice有多种写法


我在网上看到很多是json格式进行访问,可是我这里真的需要xml格式进行处理,能举个例子给我不,谢谢

 以前用jquery的AJAX里面,想要返回消息为XML还是Json可以自己设置
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: