简体   繁体   English

Ajax错误 - “权限被拒绝”

[英]Ajax error - “permission denied”

Sorry, this looks longer than it probably is but I thought I should include all the information! 对不起,这看起来比它可能更长,但我想我应该包括所有信息!

I'm using a simple Ajax script to dynamically bring content into a <div> on a page. 我正在使用一个简单的Ajax脚本来动态地将内容带入页面上的<div>。 The first request to load some new content into the div works fine, but if I've got an Ajax "back" link within the content which has just loaded, it seems to throw an error. 将一些新内容加载到div中的第一个请求工作正常,但如果我刚刚加载的内容中有一个Ajax“后退”链接,则似乎会抛出错误。

Even stranger, it works on my office network, but it fails If I'm on a home or VPN network. 更奇怪的是,它可以在我的办公室网络上运行,但它失败如果我在家里或VPN网络上。 If it fails, this error pops up in the JavaScript debugger: 如果失败,则会在JavaScript调试器中弹出此错误:

Line: 12
Char: 11
Error: Permission Denied
Code: 0
URL: http://www.example.com/about.php

The code really isn't that complex, it's just a slightly hacked around version of the stuff on the W3 website, but the fact that the return call is "denied" is confusing me. 代码真的不是那么复杂,它只是在W3网站上的一个有点被黑客攻击的版本,但返回调用被“拒绝”的事实令我感到困惑。 Would it be something within the server IIS configuration to stop scripting attacks? 是否可以在服务器IIS配置中停止脚本攻击? (Random thought?) (随机思考?)

Any help appreciated ;) 任何帮助赞赏;)

First - the Ajax script 首先 - Ajax脚本

var myHttpRequest = false;
if(window.XMLHttpRequest)
     myHttpRequest = new XMLHttpRequest();
else if(window.ActiveXObject)
     myHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
function loadContent(source, content)
{
     if(myHttpRequest)
     {
          var data = document.getElementById(content);
          myHttpRequest.open("GET",source);
            data.innerHTML = '<div class=\"loading_image\"><img src=\"images/loading.gif\" width=\"54px\" height=\"55px\" alt="loading" /></div>';
          myHttpRequest.onreadystatechange = function()
          {
               if(myHttpRequest.readyState==4)
                    data.innerHTML = myHttpRequest.responseText;
                    $('#col2_2_content').supersleight();
          }
          myHttpRequest.send(null);
     }
}

Then this is a truncated example of the page which calls the Ajax content and has the col2_2_content Div where everything gets inserted. 然后,这是一个截断的示例,该页面调用Ajax内容并具有col2_2_content Div,其中插入了所有内容。 The file ajax.js is referenced in the head section. 头部分引用了文件ajax.js

<div id="col2_2_content">
    <div class="mugshot_container">
        <img src="images/mugshot_dh.jpg" onClick="loadContent('about/dh.php?ajax=yes', 'col2_2_content');"/>
    </div>
</div>

And this is the code from about/dh.php which is inserted via Ajax, along with the PHP formatting to decide what should be returned. 这是来自about/dh.php的代码,它通过Ajax插入,以及PHP格式来决定应该返回什么。 (It's designed to be called directly too - in which case it gets a header/footer wrapped around). (它的设计也是直接调用的 - 在这种情况下它会得到一个页眉/页脚)。

<?php
$home_url = "http://www.example.com/url/";
$content = "
    <p>Some Text</p>
    <p><a onClick=\"loadContent('$home_url/about/about-main.php?ajax=yes',  'col2_2_content');\">Back</a></p>
";
if (isset($_REQUEST['ajax']) ) {
    echo $content;
} else {
    include_once 'about-header.php';
    echo $content;
    include_once 'about-footer.php';
}
?>

“许可被拒绝”听起来很可疑,好像你有同样的原始政策问题。

You got yourself in cross-domain request situation. 你有自己的跨域请求情况。 AJAX requests basically can be made only to the server that served the page. AJAX请求基本上只能用于服务页面的服务器。 So if your script page is loaded from http://website.com/url , you can make any call to http://website.com/ but any call to http://url.com would fail. 因此,如果您的脚本页面是从http://website.com/url加载的,您可以拨打http://website.com/但任何对http://url.com调用都将失败。

Having said that, it is possible to call http://s1.example.com from http://s2.example.com if you run document.domain = "example.com" . 话虽如此,如果您运行document.domain = "example.com" ,可以从http://s2.example.com调用http://s1.example.com

But if you really need to access data across domains, there's few ways to do that. 但是,如果您确实需要跨域访问数据,那么几乎没有办法。 Simplest one I know is to use <script> tag to do the query. 我知道的最简单的方法是使用<script>标签来进行查询。 You can edit your document to add <script> tag with any src you like and browser will go there and fetch the script for you. 您可以编辑您的文档,将<script>标记添加到您喜欢的任何src ,浏览器将转到那里并为您获取脚本。 So if you control http://url.com , you can just make it create a javascript instead of HTML page and this script would be loaded and executed. 因此,如果你控制http://url.com ,你可以让它创建一个javascript而不是HTML页面,这个脚本将被加载和执行。 This method is used to make JSONP work. 此方法用于使JSONP工作。

Cross-site scripting might work without security issues in the local network because IE doesn't put that much restrictions in that case. 跨站点脚本可能在本地网络中没有安全问题的情况下工作,因为IE在这种情况下没有那么多限制。 I doubt though it will work in any other browser even in your LAN. 我怀疑它甚至可以在你的局域网中的任何其他浏览器中工作。

Is the $home_url located on the same domain as the page that is making the XMLHTTP request? $home_url是否与发出XMLHTTP请求的页面位于同一个域中?

The permission denied error is almost always because of an attempt to request content across domains or security zones... ( http://msdn.microsoft.com/en-us/library/ms537505%28VS.85%29.aspx#xdomain ) Especially since you're seeing it over the VPN and not in the office, this sounds like it could be the issue. 权限被拒绝错误几乎总是因为尝试跨域或安全区域请求内容...( http://msdn.microsoft.com/en-us/library/ms537505%28VS.85%29.aspx#xdomain特别是因为你通过VPN而不是办公室看到它,这听起来可能是问题所在。

I'd fire up Firebug or some other debugging tool that allows you to see what the request is. 我会启动Firebug或其他一些调试工具,让您查看请求是什么。

Given all the answers already posted. 鉴于已发布的所有答案。 I'd suggest you use relative URLs. 我建议你使用相对的URL。 XMLHttpRequest can go cross domain in latest browsers I believe. 我相信XMLHttpRequest可以在最新的浏览器中跨域。 The specifications for this was released a while ago by W3C. W3C不久前发布了这个规格。

I believe this is it: http://www.w3.org/TR/access-control/ 我相信就是这样: http//www.w3.org/TR/access-control/

You can also use dynamic script tags if you want to go cross domain. 如果要跨域,也可以使用动态脚本标记。 That seems to be the most popular. 这似乎是最受欢迎的。 Usually, this is implemented with the javascript page writing a callback, with JSON as the parameter. 通常,这是通过javascript页面编写回调来实现的,以JSON作为参数。

There are numerous other ways to enable cross domain http in the browser, but they all involve prior setup, unless you use a proxy. 在浏览器中还有许多其他方法可以启用跨域http,但它们都涉及事先设置,除非您使用代理。 You can actually use a proxy that responds with Javascript, so the proxy can also be on a remote domain. 您实际上可以使用通过Javascript响应的代理,因此代理也可以位于远程域上。

I have an example here: 我在这里有一个例子:

http://json-proxy.jgate.de/ http://json-proxy.jgate.de/

Here is another specifically set up to consume XML resources and respond with JSON. 这是另一个专门设置为使用XML资源并使用JSON进行响应的设置。

http://jsonproxy.appspot.com/ http://jsonproxy.appspot.com/

if ($_SERVER['HTTP_HOST'] == "mysite.com") {
$home_url = "http://mysite.com/testing/rebrand/";
} else {
$home_url = "http://www.mysite.com/testing/rebrand/";   
}

Can't post this in a comment, so putting it here ;) 无法在评论中发布此内容,因此请将其放在此处;)

This did the trick - I added this in config.php to differentiate between the domains. 这样做了 - 我在config.php中添加了这个以区分域。 Everything now works A-OK. 一切现在都可以正常运行。 Thanks guys :) 多谢你们 :)

Note -- Note 注 - 注意

do not use " http://www.domain.xxx " or " http://localhost/ " or "IP >> 127.0.0.1" for URL in ajax. 对于ajax中的URL,请勿使用“ http://www.domain.xxx ”或“ http:// localhost / ”或“IP >> 127.0.0.1”。 only use path(directory) and page name without address. 仅使用没有地址的路径(目录)和页面名称。

false state: 虚假状态:

var AJAXobj = createAjax();
AJAXobj.onreadystatechange = handlesAJAXcheck;
AJAXobj.open('POST', 'http://www.example.com/dir1/dir2/page.php', true);
AJAXobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
AJAXobj.send(pack);

true state: 真实状态:

var AJAXobj = createAjax();
AJAXobj.onreadystatechange = handlesAJAXcheck;
AJAXobj.open('POST','dir1/dir2/page.php', true);   // <<--- note
AJAXobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
AJAXobj.send(pack);

function createAjax()
{
    var ajaxHttp = null;
    try
    {
        if(typeof ActiveXObject == 'function')
            ajaxHttp = new ActiveXObject("Microsoft.XMLHTTP");
        else 
        if(window.XMLHttpRequest)
            ajaxHttp = new XMLHttpRequest();
    }
    catch(e)
    {
        alert(e.message);
        return null;
    }
    //-------------
    return ajaxHttp;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM