GVKun编程网logo

起源 是Access-Control-Allow-Origin不允许的(起源source)

9

在这篇文章中,我们将为您详细介绍起源是Access-Control-Allow-Origin不允许的的内容,并且讨论关于起源source的相关问题。此外,我们还会涉及一些关于$http.get被Acc

在这篇文章中,我们将为您详细介绍起源 是Access-Control-Allow-Origin不允许的的内容,并且讨论关于起源source的相关问题。此外,我们还会涉及一些关于$ http.get被Access-Control-Allow-Origin不允许,但$ .ajax是、Access-Control-Allow-Origin 不允许 Origin null、Access-Control-Allow-Origin 不允许来源、Access-Control-Allow-Origin 标头如何工作? - How does Access-Control-Allow-Origin header work?的知识,以帮助您更全面地了解这个主题。

本文目录一览:

起源 是Access-Control-Allow-Origin不允许的(起源source)

起源 是Access-Control-Allow-Origin不允许的(起源source)

XMLHttpRequest cannot load http://localhost:8080/api/test. Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.

我阅读了有关跨域Ajax请求的信息,并了解了潜在的安全问题。就我而言,有2台服务器在本地运行,并且希望在测试期间启用跨域请求。

localhost:8080 - Google Appengine dev serverlocalhost:3000 - Node.js server

localhost:8080 - GAEserver从节点服务器加载页面时,我向发出了一个ajax请求。什么是最简单,最安全的方法(不想使用disable-web-security选项启动chrome )。如果必须更改''Content-Type'',是否应该在节点服务器上进行更改?怎么样?

答案1

小编典典

由于它们运行在不同的端口上,因此它们是不同的JavaScript origin。它们在同一台计算机/主机名上没关系。

您需要在服务器(localhost:8080)上启用CORS。

您需要做的就是向服务器添加HTTP标头:

Access-Control-Allow-Origin: http://localhost:3000

或者,为简单起见:

Access-Control-Allow-Origin: *

如果您的服务器尝试设置Cookie,而您使用的是,请不要使用“ *” withCredentials = true

当响应凭据请求时,服务器必须指定域,并且不能使用通配符。

$ http.get被Access-Control-Allow-Origin不允许,但$ .ajax是

$ http.get被Access-Control-Allow-Origin不允许,但$ .ajax是

我从我控制的远程服务器获取json时遇到问题。我有2个Web应用程序,一个提供数据并在端口3311上运行,另一个请求数据的应用程序在端口5000上运行。

使用jquery的以下工作:

$.ajax({  url: "http://localhost:3311/get-data",  type: ''GET'',  dataType: ''json'',  beforeSend: function(xhr) {    xhr.setRequestHeader("x-some-header", "some-value");  }}).done(function(data) {     $rootScope.$apply(function() {d.resolve(data); });}).fail(function(data) {    $rootScope.$apply(function() {d.reject(data); });});

当尝试使用angular相同的获取请求时

$http    .get("http://localhost:3311/get-data", { headers: {"x-some-header": "some-value"} })    .success(function(data) { d.resolve(data);})    .error(function(data) { d.reject(data); });

我收到错误

Origin http://localhost:5000 is not allowed by Access-Control-Allow-Origin.

控制台日志显示OPTIONS请求返回HTTP200之后发生的错误

OPTIONS http://localhost:3311//get-data 200 (OK) angular.min.js:99(anonymous function) angular.min.js:99l angular.min.js:95m angular.min.js:94(anonymous function) app.js:78b.extend.each jquery-1.9.1.min.js:3b.fn.b.each jquery-1.9.1.min.js:3(anonymous function) app.js:76d angular.min.js:28instantiate angular.min.js:28(anonymous function) angular.min.js:52updateView angular-ui-states.js:892e.$broadcast angular.min.js:90transition angular-ui-states.js:324h angular.min.js:77(anonymous function) angular.min.js:78e.$eval angular.min.js:88e.$digest angular.min.js:86e.$apply angular.min.js:88e angular.min.js:94o angular.min.js:98s.onreadystatechange angular.min.js:99

从OPTIONS请求返回的标头是

HTTP/1.1 200 OKCache-Control: privateContent-Type: text/plainServer: Microsoft-IIS/8.0Access-Control-Allow-Origin: *Access-Control-Allow-Methods: GET, POST, OPTIONSAccess-Control-Allow-Headers: Content-Type, Accept, X-Requested-With, x-some-headerX-AspNet-Version: 4.0.30319X-SourceFiles: =?UTF-8?B?....X-Powered-By: ASP.NETDate: Tue, 21 May 2013 01:52:37 GMTContent-Length: 0

答案1

小编典典

这可能是由于Angular包含请求标头的默认行为所致''X-Requested-With'',这可能导致CORS问题。在v 1.1.1(不稳定分支-
参见v1.1.1错误修复)中已通过从跨域请求中删除标头来修复此问题:https
:
//github.com/angular/angular.js/issues/1004。

删除标头并使其在1.0分支上很容易。以下行将从应用程序中$ http服务完成的所有请求(不仅是CORS)中删除标头:

yourModule  .config(function($httpProvider){    delete $httpProvider.defaults.headers.common[''X-Requested-With''];});

更新 一点警告-
Angular(如jQuery)不支持IE9的CORS。IE10是第一个支持CORS的IE浏览器。此博客文章描述了在特定条件下如何在IE8 /
IE9中获得CORS支持,但不适用于Angular $ http服务:http:
//blogs.msdn.com/b/ieinternals/archive/2010/05/ 13 / xdomainrequest-
restrictions-limitations-and-
workarounds.aspx

Access-Control-Allow-Origin 不允许 Origin null

Access-Control-Allow-Origin 不允许 Origin null

我制作了一个小的 xslt 文件来创建一个名为 weather.xsl 的 html 输出,代码如下:

<!-- DWXMLSource="http://weather.yahooapis.com/forecastrss?w=38325&u=c" --><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"exclude-result-prefixes="yweather"xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"><xsl:output omit-xml-declaration="yes" indent="yes"/><xsl:strip-space elements="*"/><xsl:template match="/">    <img src="{/*/*/item/yweather:condition/@text}.jpg"/></xsl:template></xsl:stylesheet>

我想将 html 输出加载到 html 文件中的 div 中,我尝试使用 jQuery 执行如下操作:

<div id="result"><script type="text/javascript">$(''#result'').load(''weather.xsl'');</script></div>

但我收到以下错误:Access-Control-Allow-Origin 不允许 Origin null。

我已经阅读了有关向 xslt 添加标头的信息,但我不知道该怎么做,所以任何帮助都将不胜感激,如果无法以这种方式加载 html
输出,请提供有关其他方法的建议这样做会很棒。

答案1

小编典典

Originnull是本地文件系统,因此这表明您正在加载load通过file:///URL 进行调用的 HTML
页面(例如,只需在本地文件浏览器或类似文件中双击它)。

大多数浏览器将同源策略应用于本地文件,甚至禁止从与文档相同的目录加载文件。(过去是
Firefox 允许相同的目录和子目录,但不再允许.

基本上,将 ajax 与本地资源一起使用是行不通的。

如果您只是在本地测试一些您将真正部署到 Web 的东西,而不是使用本地文件,请安装一个简单的 Web 服务器并通过http://URL
进行测试。这为您提供了更准确的安全图片。您的 IDE 很可能内置了某种服务器(直接或通过扩展),让您只需在 IDE 中点击“运行”并启动服务器并提供文件。

Access-Control-Allow-Origin 不允许来源

Access-Control-Allow-Origin 不允许来源

我正在Sencha Touch 2
应用程序(包装在PhoneGapAjax.request中)中创建远程
PHP
服务器。

来自服务器的响应如下:

XMLHttpRequest
无法加载http://nqatalog.negroesquisso.pt/login.php。Access-
Control-Allow- Originhttp://localhost:8888不允许 Origin。

我该如何解决这个问题?

Access-Control-Allow-Origin 标头如何工作? - How does Access-Control-Allow-Origin header work?

Access-Control-Allow-Origin 标头如何工作? - How does Access-Control-Allow-Origin header work?

问题:

Apparently, I have completely misunderstood its semantics. 显然,我完全误解了它的语义。 I thought of something like this: 我想到了这样的事情:

  1. A client downloads javascript code MyCode.js from http://siteA - the origin . 客户端从 http://siteA- origin 下载 javascript 代码 MyCode.js。
  2. The response header of MyCode.js contains Access-Control-Allow-Origin: http://siteB , which I thought meant that MyCode.js was allowed to make cross-origin references to the site B. MyCode.js 的响应标头包含 Access-Control-Allow-Origin:http:// siteB ,我认为这意味着 MyCode.js 被允许对站点 B 进行跨域引用。
  3. The client triggers some functionality of MyCode.js, which in turn make requests to http://siteB, which should be fine, despite being cross-origin requests. 客户端触发了 MyCode.js 的某些功能,该功能继而向 http://siteB 发出了请求,尽管这是跨域请求,但仍然可以。

Well, I am wrong. 好吧,我错了。 It does not work like this at all. 它根本不像这样工作。 So, I have read Cross-origin resource sharing and attempted to read Cross-Origin Resource Sharing in w3c recommendation 因此,我阅读了跨域资源共享,并尝试阅读 w3c 建议中的跨域资源共享

One thing is sure - I still do not understand how am I supposed to use this header. 可以确定的一件事 - 我仍然不明白我应该如何使用此标头。

I have full control of both site A and site B. How do I enable the javascript code downloaded from the site A to access resources on the site B using this header? 我对站点 A 和站点 B 都拥有完全控制权。如何使用此标头使从站点 A 下载的 javascript 代码能够访问站点 B 上的资源?

PS 聚苯乙烯

I do not want to utilize JSONP. 我不想利用 JSONP。


解决方案:

参考一: https://stackoom.com/question/id4F/Access-Control-Allow-Origin 标头如何工作
参考二: https://oldbug.net/q/id4F/How-does-Access-Control-Allow-Origin-header-work

关于起源 是Access-Control-Allow-Origin不允许的起源source的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于$ http.get被Access-Control-Allow-Origin不允许,但$ .ajax是、Access-Control-Allow-Origin 不允许 Origin null、Access-Control-Allow-Origin 不允许来源、Access-Control-Allow-Origin 标头如何工作? - How does Access-Control-Allow-Origin header work?的相关知识,请在本站寻找。

本文标签: