访问受限的URI拒绝代码:1012

时间:2020-03-05 18:49:52  来源:igfitidea点击:

我们如何解决FireFox 3上的Ajax跨站点脚本问题?

解决方案

回答

如果我们使用的是jQuery,则可以使用回调函数来克服此问题:

http://docs.jquery.com/Ajax/jQuery.ajax#options

As of jQuery 1.2, you can load JSON
  data located on another domain if you
  specify a JSONP callback, which can be
  done like so: "myurl?callback=?".
  jQuery automatically replaces the ?
  with the correct method name to call,
  calling your specified callback. Or,
  if you set the dataType to "jsonp" a
  callback will be automatically added
  to your Ajax request.

或者,我们可以向服务器端脚本发出ajax请求,该脚本将为我们执行跨域调用,然后将数据传递回脚本

回答

一些更多的细节会很不错:我们正在使用哪个AJAX库,我们想达到什么目标,如何实现。

例如,它可以是跨域Ajax请求,这是不允许的。在这种情况下,请使用JSON。

回答

我最近遇到了这个问题,当时是当我作为AJAX加载本地请求时,而不是跨站点脚本问题。而且,吉米本人似乎也有同样的问题。这似乎是FF安全问题,本文介绍了访问受限uri拒绝的原因和解决方案"代码:" 1012问题。

Sorry, got that error using JQuery
  $.ajax on FireFox 3. Tried jsonp
  suggestion but I think that will only
  work with something that will serve up
  json. I'm trying to create a sample
  local html file based mashup that will
  pull data from Yahoo!Finance, but they
  are serving .csv, so I think I'm SOL.
  – Jimmy Chandra (Sep 9 at 17:20)

希望我们会发现它有用。

回答

要更新答案(我想,以后再寻找该答案,主要是出于我的利益),如果正在加载XML或者其他内容,我们可以随时询问用户是否允许我们使用此代码从其他站点读取:

try {
    if (netscape.security.PrivilegeManager.enablePrivilege)
        netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
} catch (e) { 
    alert("Sorry, browser security settings won't let this program run."); 
    return; 
}

(来自RESTful Web服务书)但是,当从本地文件加载html文件时,这仅在firefox中有效。所以,没有用。

回答

另一种解决方案:如果我们只需要标头,则可以将" HEAD"指定为方法,并且不会触发安全问题。例如,如果我们只想知道网页是否存在。

var client = new XMLHttpRequest();
client.open("HEAD", my_url, false);
client.send(null);
if(client.readyState != 4 || client.status != 200) //if we failed
    alert("can't open web page");