ajax调用webservice返回xml文档问题。

nkhuangyanping 2011-06-20 02:42:23
我要访问一个网页,但是因为跨域问题,在访问期间出现访问的内容不全。所以我在后台用webservice访问网页,并得到网页内容,然后返回string类型的内容
代码如下:

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{

public WebService()
{

//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}

[WebMethod]
public string GetXML(string MyXMLurl)
{
string returnStr = "";
try
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
string xml = string.Empty;
System.Net.HttpWebRequest all_codeRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(MyXMLurl);
System.Net.HttpWebResponse all_codeResponse = (System.Net.HttpWebResponse)all_codeRequest.GetResponse();
if (all_codeResponse.StatusCode == System.Net.HttpStatusCode.OK)
{
string tempstr = all_codeResponse.ContentEncoding;
System.IO.StreamReader the_Reader = new System.IO.StreamReader(all_codeResponse.GetResponseStream(), System.Text.Encoding.UTF8);
xml = the_Reader.ReadToEnd();
doc.LoadXml(xml);
///根据里面的节点 自己 解析
System.Xml.XmlNode node = doc.SelectSingleNode("result");
string nodeStr = node.InnerXml;
System.Xml.XmlNodeList nodeList = node.ChildNodes;
//保存xml文件
doc.Save(@"./XDCPGWeb/XML/proxyHandler.xml");
}
return xml;
}
catch
{
return returnStr;
}
}

}


然后再js上调用的时候是这样的:

DisplayRoute = function(XMLurl){
try
{
var pointList = [];
$(document).ready(function(){
$.ajax(
{
type:"post",
url:"WebService.asmx/GetXML",
data:{MyXMLurl:XMLurl},
cache:false,
success: function(data) {
var myData=data.documentElement.textContent;
if (window.DOMParser)//firefox内核的浏览器
{
alert(1);
parser=new DOMParser();
xmlDoc=parser.parseFromString(myData,"text/xml");
}
else // Internet Explorer
{
alert(2);
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
// xmlDoc.async="false";
xmlDoc.loadXML(myData);
}
var items = xmlDoc.getElementsByTagName("routelatlon").length;
alert(3);
for(var i =0;i<items;i++){
var ll ;
if(!xmlDoc.getElementsByTagName("routelatlon")[i].text){
alert(4);
ll = xmlDoc.getElementsByTagName("routelatlon")[i].textContent.split(";");
}else{
alert(5);
ll = xmlDoc.getElementsByTagName("routelatlon")[i].text.split(";");
}
var length = ll.length;
for(var j = 0; j<parseInt(length)-1;j++){
var lonlat = ll[j];
var lonlat = lonlat.split(",");
var newPoint = new GeoSurf.Geometry.Point(lonlat[0],lonlat[1]);
pointList.push(newPoint);
}
alert(7);
var linearRing = new GeoSurf.Geometry.LinearRing(pointList);
DC.Route.lineFeature = new GeoSurf.Feature.Vector(new GeoSurf.Geometry.LineString(pointList),null,DC.Route.style_blue);
DC.Route.lineFeature.style.strokeColor ="#E60000";
DC.Route.routeLayer.addFeatures([DC.Route.lineFeature]);
}
}
});
});
}
catch (oError)
{
alert(oError);
}
}


但是在ie浏览器下alter(data)只是显示[object],而火狐的话弹出的是[object XMLDocument]
在火狐浏览器下能正常运行出xml文档,但是在ie下不行,请问是怎么回事?
...全文
217 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
nkhuangyanping 2011-06-25
  • 打赏
  • 举报
回复
用了别的方法解决了。
谢谢大家
  • 打赏
  • 举报
回复
IE没有textContent属性

居然你返回的都是xml了,还重新用Microsoft.XMLDOM加载做什么,直接使用就行了。。有点多余了
nkhuangyanping 2011-06-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 aspwebchh 的回复:]

http://www.nczonline.net/downloads/zXml.zip
用用这个xml库
dom,xpath,xslt,ajax等各浏览器支持的都还可以
[/Quote]
这个会用了,但是还是不能解决问题,ie上根本没返回xml内容,别的浏览器都返回
nkhuangyanping 2011-06-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 aspwebchh 的回复:]

http://www.nczonline.net/downloads/zXml.zip
用用这个xml库
dom,xpath,xslt,ajax等各浏览器支持的都还可以
[/Quote]
问一下,怎么用呀?
nkhuangyanping 2011-06-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zell419 的回复:]

dataType :"xml"
加上试试 ~
[/Quote]
我的是win7的ie8,我加了还是不行
挨踢直男 2011-06-20
  • 打赏
  • 举报
回复
http://www.nczonline.net/downloads/zXml.zip
用用这个xml库
dom,xpath,xslt,ajax等各浏览器支持的都还可以
zell419 2011-06-20
  • 打赏
  • 举报
回复
dataType :"xml"
加上试试 ~

52,782

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧