相关疑难解决方法(0)

为什么我的JavaScript在所请求的资源上出现"No'Access-Control-Allow-Origin'标头"错误,当Postman没有?

我试图通过连接到Flask内置的RESTful API来使用JavaScript进行授权.但是,当我发出请求时,我收到以下错误:

XMLHttpRequest无法加载http:// myApiUrl/login.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许原点'null'访问.

我知道API或远程资源必须设置标题,但为什么在我通过Chrome扩展程序Postman发出请求时它可以正常工作?

这是请求代码:

$.ajax({
    type: "POST",
    dataType: 'text',
    url: api,
    username: 'user',
    password: 'pass',
    crossDomain : true,
    xhrFields: {
        withCredentials: true
    }
})
    .done(function( data ) {
        console.log("done");
    })
    .fail( function(xhr, textStatus, errorThrown) {
        alert(xhr.responseText);
        alert(textStatus);
    });
Run Code Online (Sandbox Code Playgroud)

javascript jquery same-origin-policy cors flask-restless

2320
推荐指数
29
解决办法
399万
查看次数

如何允许CORS?

我试图在我的Node.js应用程序中支持使用Express.js Web框架的CORS.我已经阅读有关如何处理此问题的Google小组讨论,并阅读了一些有关CORS如何工作的文章.首先,我这样做了(代码是用CoffeeScript语法编写的):

app.options "*", (req, res) ->
  res.header 'Access-Control-Allow-Origin', '*'
  res.header 'Access-Control-Allow-Credentials', true
  # try: 'POST, GET, PUT, DELETE, OPTIONS'
  res.header 'Access-Control-Allow-Methods', 'GET, OPTIONS'
  # try: 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept'
  res.header 'Access-Control-Allow-Headers', 'Content-Type'
  # ...
Run Code Online (Sandbox Code Playgroud)

它似乎不起作用.好像我的浏览器(Chrome)没有发送初始OPTIONS请求.当我刚刚更新资源块时,我需要将跨源GET请求提交到:

app.get "/somethingelse", (req, res) ->
  # ...
  res.header 'Access-Control-Allow-Origin', '*'
  res.header 'Access-Control-Allow-Credentials', true
  res.header 'Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS'
  res.header 'Access-Control-Allow-Headers', 'Content-Type'
  # ...
Run Code Online (Sandbox Code Playgroud)

它有效(在Chrome中).这也适用于Safari.

我读过......

在实现CORS的浏览器中,每个跨源GET或POST请求前面都有一个OPTIONS请求,用于检查GET或POST是否正常.

所以我的主要问题是,为什么在我的案例中似乎没有发生这种情况?为什么我的app.options块没有被调用?为什么我需要在主app.get块中设置标题?

node.js cors coffeescript express

609
推荐指数
17
解决办法
57万
查看次数

XMLHttpRequest无法加载XXX没有'Access-Control-Allow-Origin'标头

TL;博士; 关于同源政策

我有一个Grunt进程,它启动了express.js服务器的实例.刚刚开始提供空白页面,并且Chrome中的开发人员控制台的错误日志中出现以下内容(最新版本),这一点工作非常精细:

XMLHttpRequest无法加载https://www.example.com/ 请求的资源上没有"Access-Control-Allow-Origin"标头.因此不允许来源" http:// localhost:4300 "访问.

什么阻止我访问该页面?

javascript same-origin-policy cors

92
推荐指数
2
解决办法
9万
查看次数

如何在Angular 5中的头文件中添加CORS请求

我已经在标题中添加了CORS,但我仍然在我的请求中收到了CORS问题.在标头中添加和处理CORS和其他请求的正确方法是什么?

这是服务文件代码:

import { HttpClient, HttpHeaders, HttpClientModule } from '@angular/common/http';
const httpOptions = {
  headers: new HttpHeaders({ 
    'Access-Control-Allow-Origin':'*',
    'Authorization':'authkey',
    'userid':'1'
  })
};

public baseurl = 'http://localhost/XXXXXX';

userAPI(data): Observable<any> {
  return this.http.post(this.baseurl, data, httpOptions)
    .pipe(
      tap((result) => console.log('result-->',result)),
      catchError(this.handleError('error', []))
    );
}
Run Code Online (Sandbox Code Playgroud)

错误:

对预检请求的响应未通过访问控制检查:请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许来源' http:// localhost:4200 '访问

失败:(未知网址)的Http失败响应:0未知错误

在我的服务器端代码中,我在索引文件中添加了CORS.

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
Run Code Online (Sandbox Code Playgroud)

angular angular5

63
推荐指数
4
解决办法
22万
查看次数

Angular 2 app中没有'Access-Control-Allow-Origin'标题

对于这个项目,我只是学习和练习Angular 2.我没有服务器端,并且正在向barchart ondemand api发出API请求 .

我想知道是否有可能绕过cors问题.我对这一切仍然相当新,所以非常感谢宝贝步骤说明!我正在使用http://localhost:8080.

错误信息:( api键已注释掉)

XMLHttpRequest cannot load http://marketdata.websol.barchart.com/getHistory.json?key=MY_API_KEY&symbol=GOOG&type=daily&startDate=20150311000000. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

StockInformationService:

import {Injectable} from 'angular2/core';
import {Http, Headers} from 'angular2/http';
import {Observable} from 'rxjs/Rx';

@Injectable()
export class StockInformationService {
    private apiRoot = "http://marketdata.websol.barchart.com/getHistory.json?key=MY_API_KEY&";

    constructor (private _http: Http) {}

    getData(symbol: string): Observable<any> {
        // Tried adding headers with no luck …
Run Code Online (Sandbox Code Playgroud)

header cors webpack angular

46
推荐指数
6
解决办法
19万
查看次数

AngularJS:请求的资源上没有"Access-Control-Allow-Origin"标头

我正在写我的webApp,而且我正在使用AngularJS.在这个应用程序中,我创建了一个名为script.js的文件,并报告此代码:

var modulo = angular.module('progetto', ['ngRoute']);

    // configure our routes
    modulo.config(function ($routeProvider, $httpProvider) {
        $routeProvider

            // route for the home page
            .when('/', {
                templateUrl: 'listaFilm.html',
                controller: 'listaController'
            })

            // route for the description page
            .when('/:phoneName', {
                templateUrl: 'description.html',
                controller: 'descriptionController'
            });


            $httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = '*';

    });


    modulo.controller('listaController', function ($scope, $http) {
        $http.get('https://api.getevents.co/event?&lat=41.904196&lng=12.465974').success(function (data) {
            $scope.names = data;
            }).
            error(function (data, status) {
                $scope.names = "Request failed";
            });
    });
Run Code Online (Sandbox Code Playgroud)

使用此代码,我按照RESTful原则调用API.当我运行代码时,我遇到了这个问题:

XMLHttpRequest无法加载https://api.getevents.co请求的资源上没有"Access-Control-Allow-Origin"标头.原产地" 的http://本地主机:8383 "因此不允许访问.

在网上阅读我明白我有一个叫CORS的问题...我已经尝试了几个解决方案,但我没有解决问题.
我该如何解决这个问题?
我必须添加什么代码来修复它?

html javascript xmlhttprequest cors angularjs

45
推荐指数
2
解决办法
30万
查看次数

Angular 客户端启用 CORS

CORS 在服务器上很好,并且按预期工作。我尝试使用 angular HTTPClient 向服务器的 REST API 发送请求,但收到 CORS 错误。如果在服务器上启用了 CORS,为什么会出现此错误?客户端应该没问题吧?

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/api/blah/blah (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
Run Code Online (Sandbox Code Playgroud)

我怎样才能在这个请求上启用 CORS..... 在此处输入图片说明

node.js cors express angular

6
推荐指数
1
解决办法
1万
查看次数

没有“ Access-Control-Allow-Origin”标头存在。.角度7和表示

我在端口3000上运行带有express的服务器,在端口4200上运行带有angular 7的客户端。发出请求后,遇到了CORS问题。

通过CORS策略已阻止从源“ http:// localhost:4200 ” 访问“ http:// localhost:3000 / drug ” 上的XMLHttpRequest 在请求的资源上不存在“ Access-Control-Allow-Origin”标头。

我已经在线尝试了所有解决方案,例如使用cors软件包,并在路由器之前设置了中间件,如以下代码所示(Angular 6-请求资源上没有'Access-Control-Allow-Origin'标头)。但是它仍然没有解决,并不断出现同样的错误。有人会遇到无法用所有解决方案解决CORS的问题吗?请你帮助我好吗?

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
  res.header(
    "Access-Control-Allow-Header",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  next();
});

app.use((req, res, next) => {
      res.header("Access-Control-Allow-Origin", "http://localhost:4200");
      res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
      res.header(
        "Access-Control-Allow-Header",
        "Origin, X-Requested-With, Content-Type, Accept"
      );
      next();
    });
Run Code Online (Sandbox Code Playgroud)

node.js express cross-origin-read-blocking angular7

2
推荐指数
1
解决办法
1万
查看次数

如何为角度5 / nodejs启用Access-Control-Allow-Origin?

阅读包括“ Access-Control-Allow-Origin”在内的许多方法,但没有一种对我有用。

我使用@ angular / common / http模块和外部url作为数据源。通过尝试获取数据来获取错误:///// .................

Failed to load http://accounts.......com/accounts: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 503.
Run Code Online (Sandbox Code Playgroud)


account.service.ts:

Failed to load http://accounts.......com/accounts: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 503.
Run Code Online (Sandbox Code Playgroud)

app.module.ts

import { Injectable                    } from '@angular/core';
import { Router                        } from '@angular/router';
import { HttpClient, …
Run Code Online (Sandbox Code Playgroud)

http node.js webpack-dev-server angular angular5

0
推荐指数
1
解决办法
3万
查看次数