无法在谷歌浏览器上使用XMLHttpRequest加载动态生成的XML
本文关键字:动态 加载 XML XMLHttpRequest 谷歌浏览器 | 更新日期: 2023-09-27 18:30:23
我有一个生成XML输出的aspx文件。在另一页上,我尝试使用 XMLHttpRequest open 方法来读取从 aspx 文件输出的 XML。FF,IE,Safari似乎都可以工作,除了Chrome。但是在 chrome 上,如果我直接转到地址栏上的 aspx 页面,则 xml 会按预期生成。任何帮助,不胜感激。
下面是 aspx 代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProviderFinderXML.aspx.cs" Inherits="Pages_ProviderFinderXML" %>
<%@ OutputCache Duration="6" VaryByParam="Type" %>
Aspx 代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Text;
using System.Data;
public partial class Pages_ProviderFinderXML : System.Web.UI.Page
{
protected string appPath = Helper.GetApplicationPath();
private DataTable dataTable, dataTable2;
private Helper helper = new Helper();
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnInit(EventArgs e)
{
float center_lat = 0, center_lng = 0;
int radius = 0;
if (Request.QueryString["lat"] != null)
center_lat = Common.ConvertToFloat(Request.QueryString["lat"].ToString());
if (Request.QueryString["lng"] != null)
center_lng = Common.ConvertToFloat(Request.QueryString["lng"].ToString());
if (Request.QueryString["radius"] != null)
radius = Common.ConvertToInt(Request.QueryString["radius"].ToString());
int categoryId = 0;
if (Request.QueryString["categoryId"] != null)
categoryId = Common.ConvertToInt(Request.QueryString["categoryId"].ToString());
Response.Buffer = false;
Response.Clear();
Response.ContentType = "application/xml";
XmlTextWriter xmlWriter = new XmlTextWriter(Response.Output);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("markers");
int perPage = 0, currentPage = 1;
dataTable = helper.DIR_GetNearbyDirectoryMembersByLatLng(center_lat, center_lng, radius, categoryId, perPage, currentPage);
if (dataTable.Select().Length > 0)
{
xmlWriter.WriteAttributeString("title", cRow["title"].ToString().Replace("'", "'''"));
}
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
xmlWriter.Close();
base.OnInit(e);
Response.Close();
}
private string GetFullyQualifiedUrl(string url)
{
return string.Concat(Request.Url.GetLeftPart(UriPartial.Authority), ResolveUrl(url));
}
}
下面是调用该 aspx 页的测试页代码部分:
<script type="text/javascript">
$(document).ready(function () {
searchLocationsNear();
});
function searchLocationsNear() {
var searchUrl = 'http://localhost/integrativediagnosis2/Pages/ProviderFinderXML.aspx?lat=42.3584308&lng=-71.0597732&radius=20';
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");
}
xmlhttp.open("GET", "ProviderFinderXML.aspx?lat=42.3584308&lng=-71.0597732&radius=20", false);
xmlhttp.send();
}
</script>
使用 JQuery.ajax 代替手工调用。
使用 Fiddler 或其他 HTTP 跟踪工具确认所有请求都按预期发送/接收。
我没有立即看到任何错误。没有代码和读取输出,因此不清楚"在Chrome中不起作用"是什么意思。