当地的AJAX调用远程站点工作在Safari浏览器,但不是在其他浏览器 [英] local AJAX-call to remote site works in Safari but not in other browsers

查看:304
本文介绍了当地的AJAX调用远程站点工作在Safari浏览器,但不是在其他浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持认为使用JavaScript网站。该脚本使用jQuery和从加载在其网站通常是托管服务器上的某些内容。

I am maintaining a website that uses Javascript. The script uses jQuery and loads some content from the server at which the site is normally hosted.

只是为了方便,同时维持该网站,我对我的iMac上运行该网站的本地副本。这工作完全正常,当我使用Safari浏览器。但是,Firefox,Opera和Chrome浏览器拒绝工作。我想这是因为跨域策略。 (我无法与IE浏览器进行测试,因为IE浏览器在虚拟机上我的iMac上运行,因此,对于这个原因,它不能访问任何本地文件)

Just for convenience while maintaining the site, I run a local copy of the site on my iMac. This works perfectly fine when I use Safari. But Firefox, Opera and Chrome refuse to work. I guess it is because of cross-domain-policy. (I couldn't test this with IE, because IE has to run in a virtual machine on my iMac, so for this reason it is not possible to access any local files)

有Firefox和其他浏览器内的设置,我可以告诉大家,这是确定到位于远程服务器上本地的HTML页面与本地JavaScript的AJAX加载的文件浏览器?

Is there a setting within Firefox and the other browsers where I can tell the browser that it is ok to ajax-load files that are located on a remote server from a local html-page with a local javascript?

在一言以蔽之:这是我的HTML页面:

In a nutshell: This my html-page:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>some title</title>
        <link rel="stylesheet" type="text/css" href="../css/stylesheet.css">
        <script src="../js/jquery-2.1.3.min.js"></script>
        <script src="../js/myScript.js"></script>
    </head>
    <body>
        <!-- some content with a div-container to receive the ajax-content -->
    </body>
</html>

这是myScript.js:

This is myScript.js:

var errorMsg = function (msg) {
    //insert the message into the html-page
};

var JSONerror = function (jqXHR, textStatus, errorThrown ) {
    var msg = 'JSON-answer: '+jqXHR.responseText;
    msg += '<br>'+'JSON-Errorstatus: '+textStatus;
    if ($.type(errorThrown) === 'string') {
        msg += '<br>'+'Error: '+errorThrown;
    }
    errorMsg(msg);
};

var JSONreceive = function (JSONobj, StatusString, jqXHR) {
    //insert the data in JSONobj into the html-page
}

var StartAJAX = function () {
    $.ajax({
        url: 'http://my.domain.tld/cgi-bin/myPerlScript.pl',
        data: "lastID=" + lastID
           + '&qkz=' + Math.random(),
           dataType: "json",
           success: JSONreceive,
           error: JSONerror
    });
};

此外,还有一个事件监听器,监听页面滚动和缩放,并检查了一些其他方面的限制(如:是否有正在进行中的AJAX调用?)。该侦听器调用 StartAJAX

当它调用 StartAJAX 在我的网页的本地副本:在Safari浏览器(文件///用户/ ...),我得到了Ajax的内容完全没有问题插入我的HTML文档。在其他浏览器我得到的错误信息插入到HTML页面。它是:

When it calls StartAJAX on a local copy of my page (file:///User/...) within Safari, I get the Ajax-content perfectly fine inserted into my html-document. within the other browsers i get the error-message inserted into the html-page. It is:

JSON-答:未定义
  JSON-ErrorStatus中:错误
  错误:

JSON-Answer: undefined
JSON-Errorstatus: error
Error:

为什么它在Safari工作,但不能在Firefox,Chrome和Opera?

我怎样才能使这些浏览器的工作?

(我需要与所有浏览器进行测试,因为所有的浏览器呈现相同的HTML domument不同,但我不希望所有的文件上传到以后的每一个变化只是为了测试它的服务器。)

(I need to test it with all browsers, because all browsers render the same html-domument differently, but I don't want to upload all files to the server after every change just to test it.)

阅读一些答案后,我想做出点明确,那我显然没有做好充分明确:

After reading some answers, I want to make something clear, that I obviously did not make clear enough:

我在化网页浏览器搜索设置

  1. 我不会改变我的远程Web服务器的设置(阿帕奇)
  2. 我不会操纵我的远程机器上的任何文件(的.htaccess)
  3. 我会不会建立一个Web服务器在我的本地的iMac
  4. 我不会改变AJAX的电话在我的Javascript文件
  5. 的code
  6. 我不会改变的Perl脚本的code我的远程服务器上
  1. I will NOT change the settings of my remote webserver (Apache)
  2. I will NOT manipulate any files on my remote machine (.htaccess)
  3. I will NOT set up a webserver on my local iMac
  4. I will NOT change the code of the AJAX-calls in my Javascript-files
  5. I will NOT change the code of the Perl-Scripts on my remote Server

我可以告诉你为什么:

我只是做一个简短修的,我懒得每次操作的文件上传到远程机器后,我编辑它。 Web服务器的设置就可以了实际操作。我不想改变他们(也许完成我的工作前忘记改变)。同为脚本:你们当中有些人想要改变,做工精细,因为他们现在的部分。我们没有理由去触摸Ajax的电话,因为没有什么错与他们在生产环境中。

I am just doing a short maintainance, and i am too lazy to upload every manipulated file to the remote machine after I edited it. The settings of the webserver are fine for actual operation. I don't want to change them (and maybe forget the changes before finishing my work). Same for the scripts: Those parts that some of you want to change work fine as they are now. There is no reason to touch the Ajax-Calls, because there is nothing wrong with them in the productive environment.

我要的是,那些愚蠢的浏览器Firefox,Opera和Chrome浏览器的行为像Safari和正确处理Ajax的电话。

All I want is that those stupid browsers Firefox, Opera and Chrome behave like Safari and process the Ajax-calls correctly.

请谁能解释什么是如此冒险通过Ajax在Firefox,歌剧或Chrome的其他域调用数据,而这似乎是无害的做同样的事情在Safari?

Please can anyone explain what is so risky to call data via Ajax from an other domain in Firefox, Opera or Chrome while it seems to be harmless doing the same thing in Safari?

推荐答案

有一个<一个href="https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en"相对=nofollow>插件Chrome浏览器,这将迫使它忽略了安全策略。您也可以做到这一点与标志的。注意,请不要浏览真正的网络以启用此功能,因为它是为您的计算机带来安全隐患。

There is a plugin for chrome that will force it to ignore the security policy. You can also do this with flags. Note, please do not browse the "real web" with this enabled as it is a security risk for your computer.

FIREFOX

此线程的表示有presently无办法在Firefox做到这一点。

This thread indicates that there is presently no way to do this in firefox.

OPERA

再次似乎没有成为一个内置的方法忽略CORS政策

Again, there does not appear to be a built in way to ignore CORS policies.

另一种选择是让服务器( HTTP://my.domain.tld )你的情况返回正确的标题 - 特别是访问控制 - 允许 - 产地:

The alternative would be to have the server (http://my.domain.tld) in your case return the proper headers - specifically Access-Control-Allow-Origin:

这篇关于当地的AJAX调用远程站点工作在Safari浏览器,但不是在其他浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆