日期:2014-05-16  浏览次数:20774 次

用js实现Ajax,得不到返回的数据
在firefox里提示:Access to restricted URI denied (NS_ERROR_DOM_BAD_URI)
在IE里提示:SCRIPT5: 拒绝访问。
js代码是这样的:

function showTab(){
var selectedTab=this.title;
var tabs=document.getElementById("tabs").getElementsByTagName("a");
for(var i=0;i<tabs.length;i++)
{
var currentTab=tabs[i];
if(currentTab.title==selectedTab)
{
currentTab.className="active";
}
else{

currentTab.className="inactive";
}
}

var request=createRequest();
if(request==null)
{
alert("Unable create request");
}
request.onreadystatechange=showSchedule;
request.open("GET",selectedTab+".html",true);
request.send(null);

function showSchedule()
{

if(request.readyState==4){
//如果去了request.status==200,界面上能获得数据
if(request.status==200)
{

document.getElementById("content").innerHTML=request.responseText;
}else{
alert(request.status);
}
}
else{

//alert(request.status);
}
}



}

我查了下说是跨域访问的问题,我要请求的html页就在同一个文件夹下了,怎么跨域访问了呢?求解
ajax firefox 跨域访问

------解决方案--------------------
alert(selectedTab+".html")//输出什么?地址不对或者跨域了
    request.open("GET",selectedTab+".html",true);

------解决方案--------------------
通过http访问时,只能访问域名内的页面,如http://www.abc.com,只能访问www.abc.com的页面,但是不能访问http://www.bbbbb.com的

本地file浏览【就是双击或者拖入浏览器访问】时,chrome无法访问本地资源即使没跨域,ie使用acx创建的xhr能访问跨域的页面【如http://www.coding123.net/】和本地的,firefox访问本地的可以,跨域不行
------解决方案--------------------
http://hi.baidu.com/smiky/item/d6616894b418c7f0291647a1 或许有帮助