简体   繁体   English

Access-Control-Allow-Origin不允许源,jQuery Mobile

[英]Origin is not allowed by Access-Control-Allow-Origin , jquery mobile

I am developing a jQuery mobile webpage in my local server. 我正在本地服务器上开发jQuery移动网页。 I am using XAMPP so I have an Apache server. 我正在使用XAMPP,所以我有一个Apache服务器。

I am trying to import a Google Calendar in my page , but I get the error : 我正在尝试在页面中导入Google日历,但出现错误:

Origin is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin不允许起源

I found more than a million posts about it, I couldn't understand any of them. 我发现有超过一百万篇关于它的文章,我听不懂。

They are all talking about a PHP file that I need to add a header etc. I don't have any PHP files, it is all HTML and JavaScript. 他们都在谈论我需要添加标题等的PHP文件。我没有任何PHP文件,它们都是HTML和JavaScript。

I also read, that I could configure Apache so that it allowed me to make cross domain connections. 我还读到,我可以配置Apache,以便它允许我建立跨域连接。

From here I quote : 这里我引用:

If you're using Apache just add: 如果您使用的是Apache,只需添加:

 <ifModule mod_headers.c> Header set Access-Control-Allow-Origin: * </ifModule> 

in your configuration. 在您的配置中。 This will cause all responses from your webserver to be accessible from any other site on the internet. 这将使您可以从Internet上的任何其他站点访问来自Web服务器的所有响应。 If you intend to only allow services on your host to be used by a specific server you can replace the * with the URL of the originating server. 如果打算只允许特定服务器使用主机上的服务,则可以将*替换为原始服务器的URL。

Where exactly do I add this piece of code? 我到底在哪里添加这段代码? I tried to add it in the httpd.conf but nothing changed. 我试图将其添加到httpd.conf但未进行任何更改。

Any ideas? 有任何想法吗?

You can force Google Chrome to not moan about Cross-domain-origin(s) Adding this flag --disable-web-security when running chrome will allow you to test successfully. 您可以强制Google Chrome浏览器不要抱怨跨域起源。在运行chrome时添加此标志--disable-web-security可以使您成功进行测试。 I've added it to the target variable of my Chrome shortcut on my desktop like so: 我已将其添加到桌面上Chrome快捷方式的目标变量中,如下所示:

"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" --disable-web-security

Hence everytime I start Chrome it automatically is started with this flag in place and I can test cross-domain ajax calls without any issues. 因此,每次我启动Chrome时,它将自动启动并带有此标志,并且我可以测试跨域Ajax调用而不会出现任何问题。

Re-read the description of the Apache setting you provided: 重新阅读您提供的Apache设置的说明:

This will cause all responses from your webserver to be accessible from any other site on the internet. 这将使您可以从Internet上的任何其他站点访问来自Web服务器的所有响应。 If you intend to only allow services on your host to be used by a specific server you can replace the * with the URL of the originating server. 如果打算只允许特定服务器使用主机上的服务,则可以将*替换为原始服务器的URL。

In other words, this setting causes the Apache server to accept all incoming cross-domain requests of resources hosted on your server. 换句话说,这种设置使Apache服务器接受服务器上托管资源的所有传入的跨域请求。

But that is not what you want. 但这不是您想要的。 You want to access, via a cross-domain request, resources on Google's server. 您想通过跨域请求访问Google服务器上资源。 You will only be able to access these resources if they allow it. 如果这些资源允许,您将只能访问它们。 Fortunately, Google Calendar has an API that will allow you to access calendars through the methods that they've provided. 幸运的是,Google日历具有一个API,可让您通过提供的方法访问日历。 You should check it out here . 您应该在这里查看

You should enable CORS on your PHP server, try using the following code there: 您应该在PHP服务器上启用CORS,然后尝试在其中使用以下代码:

<?php
 header("Access-Control-Allow-Origin: *");

More information on how to enable CORS on your server can be found here: http://enable-cors.org/server_php.html 可在此处找到有关如何在服务器上启用CORS的更多信息: http : //enable-cors.org/server_php.html

But please consider that if you are requesting information from a server which you have no control and has no CORS enabled, you cannot make AJAX call from your JavaScript. 但是请考虑一下,如果您从无法控制且未启用CORS的服务器上请求信息,则无法从JavaScript进行AJAX调用。

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

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