首页 新闻 会员 周边

关于服务器返回xml代码

0
悬赏园豆:20 [已解决问题] 解决于 2015-06-24 22:23
  [WebMethod]
        public static string LoadDate(string type)
        {
            string str = File.ReadAllText(@"E:\xml.xml");
            HttpContext.Current.Response.ContentType = "xml";
            HttpContext.Current.Response.Charset = "UTF-8";
            return str;
        }
View Code

这上面是我的后台代码

<script type="text/javascript">
        window.onload = function () {
            loadData();
        };
        function loadData() {
            $.ajax({
                type: "POST",//通常会用到两种:GET,POST。默认是:GET
                url: "Ajax/JavascripFenYe.aspx/LoadDate",//(默认: 当前页地址) 发送请求的地址
                data: "{ type: 'loadDate' }",//组织向后台发送的数据
                dataType: "xml",//预期服务器返回的数据类型。
                beforeSend: beforeSend, //发送请求
                success: callback, //请求成功
                error: error,//请求出错 
                complete: complete,//请求完成
                cache: false,
                async: false,//默认: true) 默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。 
                contentType: "application/json;charset=utf-8"//发送服务器的编码
            });
        };
        function callback(res) {
            var doc = com.createxmldocbystr(res.d);
            var docxml = doc.lastChild;//获得xml的跟节点
            alert(docxml.firstChild);
        };
        function error() { };
        function complete() { };
        function beforeSend() { };
        function Common() {

        }
        //创建xmldoc对象
        Common.prototype.createxmldoc = function () {
            var xmlDoc = null;
            if (window.ActiveXObject) {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            }
            else if (document.implementation && document.implementation.createDocument) {
                xmlDoc = document.implementation.createDocument("", "", null);
            }
            return xmlDoc;
        }
        //通过字符串创建xmldoc对象
        Common.prototype.createxmldocbystr = function (str) {
            if (window.ActiveXObject) {
                var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
                xmlDoc.loadXML(str)
                return xmlDoc
            }
            else
                return new DOMParser().parseFromString(str, "text/xml")
        }
        //通过文件创建xmldoc对象
        Common.prototype.CreateXMLDocbypath = function (xmlFilePath) {

            if (window.ActiveXObject) {
                var oXmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
                oXmlHttp.open("get", xmlFilePath, false);
                oXmlHttp.send();
                return oXmlHttp.responseXML;

            }

            else {
                var oXmlHttp = new XMLHttpRequest();
                oXmlHttp.open("get", xmlFilePath, false);
                oXmlHttp.send(null);
                if (oXmlHttp.responseXML == null) {
                    return this.createxmldocbystr(oXmlHttp.responseText);
                }
                return oXmlHttp.responseXML;

            }
        }
        //将xml对象传化为字符串
        Common.prototype.dealxml = function (inparam) {
            var ss = "";
            if (window.ActiveXObject) {
                ss = inparam.xml;
            }
            else if (document.implementation && document.implementation.createDocument) {
                ss = (new XMLSerializer()).serializeToString(inparam);
            }
            return ss;
        }
        var com = new Common();
        //selectSingleNode 各浏览器兼容
        if (document.implementation.hasFeature("XPath", "3.0")) {
            if (typeof XMLDocument == "undefined") { XMLDocument = Document; }
            XMLDocument.prototype.selectNodes = function (cXPathString, xNode) {
                if (!xNode) { xNode = this; }
                var oNSResolver = this.createNSResolver(this.documentElement)
                var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
                var aResult = [];
                for (var i = 0; i < aItems.snapshotLength; i++) { aResult[i] = aItems.snapshotItem(i); }
                return aResult;
            }
            XMLDocument.prototype.selectSingleNode = function (cXPathString, xNode) {
                if (!xNode) { xNode = this; }
                var xItems = this.selectNodes(cXPathString, xNode);
                if (xItems.length > 0) { return xItems[0]; }
                else { return null; }
            }
            Element.prototype.selectNodes = function (cXPathString) {
                if (this.ownerDocument.selectNodes) { return this.ownerDocument.selectNodes(cXPathString, this); }
                else { throw "For XML Elements Only"; }
            }
            Element.prototype.selectSingleNode = function (cXPathString) {
                if (this.ownerDocument.selectSingleNode) { return this.ownerDocument.selectSingleNode(cXPathString, this); }
                else { throw "For XML Elements Only"; }
            }
        }
    </script>

想问下老实报这种错误

{"Message":"使用JSONJavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过了为maxJsonLength属性设置的值。","StackTrace":"在System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Objectobj,StringBuilderoutput,SerializationFormatserializationFormat)\r\n在System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Objectobj,SerializationFormatserializationFormat)\r\n在System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Objectobj)\r\n在System.Web.Script.Services.RestHandler.InvokeMethod(HttpContextcontext,WebServiceMethodDatamethodData,IDictionary`2rawParams)\r\n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContextcontext,WebServiceMethodDatamethodData)","ExceptionType":"System.InvalidOperationException"}
changsen-的主页 changsen- | 初学一级 | 园豆:29
提问于:2013-11-04 20:36
< >
分享
最佳答案
0

不建议用这种webmethod,稍不注意,都不好找错误,在哪里出的问题!远程方法还是先走aspx的load事件在,还不如直接用一般处理程序来的直接,来的快呢!

收获园豆:10
秋壶冰月 | 大侠五级 |园豆:5903 | 2013-11-04 22:02

是的,我是觉得这种方法好管理,都都可以请求到一个页面,服务端是可以返回字符,客户端也接收到了,但是不知道怎么解析

[WebMethod]
        public static void LoadDate(string type)
        {
            var Response = HttpContext.Current.Response;
            string str = File.ReadAllText(@"E:\xml.xml", System.Text.Encoding.Default);
            Response.Write("<?xml version=\"1.0\" encoding=\"gb2312\"?>" + str);
        }


服务端这样

changsen- | 园豆:29 (初学一级) | 2013-11-04 22:14

@長森-王: 你不是通过返回的字符串创建了 new ActiveXObject("Microsoft.XMLDOM")吗?然后xmlDoc.documentElement.childNode

var xmlArray=xmlDoc.documentElement.childNodes;
 for (i = 0; i < xmlArray.length; i++) {
            xmlArray[i].nodeName     //节点名称      
            xmlArray[i].childNodes[0].nodeValue // 节点内容  
}

 

s;

秋壶冰月 | 园豆:5903 (大侠五级) | 2013-11-04 22:51
其他回答(1)
0

看这错误应该是后台代码有点小问题,可能返回的并不是你所期望的XML内容,

收获园豆:10
Zery | 园豆:6151 (大侠五级) | 2013-11-04 20:53

对的,我用浏览器监控了,不知道怎么设置

支持(0) 反对(0) changsen- | 园豆:29 (初学一级) | 2013-11-04 21:02

@長森-王: 断点LoadDate方法,看str内容是什么,我觉得应该要转成xml的格式到前端。

你的异步方法接收格式都是xml的 

支持(0) 反对(0) Zery | 园豆:6151 (大侠五级) | 2013-11-04 22:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册