简体   繁体   English

无法在React中使用GraphQL查询-已被CORS策略阻止:所请求的资源上不存在“ Access-Control-Allow-Origin”标头

[英]Cannot use GraphQL queries in React - has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

I have a React application that connects to a back-end Laravel application that has a GraphQL layer on top of it. 我有一个React应用程序,该应用程序连接到在其顶部具有GraphQL层的后端Laravel应用程序。

I have a GraphQL query as such: 我有这样的GraphQL查询:

http://laravel-quarx.dev/graphql?query=query+getUserNameAndId{users{id,name}}

Which will return the following JSON: 它将返回以下JSON:

{"data":{"users":[{"id":1,"name":"Admin"},{"id":2,"name":"Saqueib Ansrai"}]}}

Here is how I fetch this data in React (I am using Redux so I have my call in the Saga): 这是我在React中获取数据的方式(我正在使用Redux,所以我在Saga中有电话):

function* fetchProducts(actions) {
    try {
        const response = yield call(fetchApi, {
            method: 'GET',
            url: '?query=query+getUserNameAndId{users{id,name}}',
        });

        yield put(productSuccess(response));
    } catch (e) {
        yield put(productFailure(e.response ? e.response.data.message : e));
    }
}

This results in the following error: 这将导致以下错误:

XMLHttpRequest cannot load http://laravel-quarx.dev/graphql/?query=query+getUserNameAndId {users{id,name}}. XMLHttpRequest无法加载http://laravel-quarx.dev/graphql/?query=query+getUserNameAndId {users {id,name}}。 Redirect from ' http://laravel-quarx.dev/graphql/?query=query+getUserNameAndId {users{id,name}}' to ' http://laravel-quarx.dev/graphql?query=query+getUserNameAndId {users{id,name}}' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 从' http://laravel-quarx.dev/graphql/?query=query+getUserNameAndId {users {id,name}}'重定向到' http://laravel-quarx.dev/graphql?query=query+getUserNameAndId { users {id,name}}已被CORS策略阻止:所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost:3000 ' is therefore not allowed access. 因此,不允许访问源' http:// localhost:3000 '。

在此处输入图片说明

I am unsure as to what I have to do on my server-side application to bypass the CORS limitations, any ideas? 我不确定要在服务器端应用程序上做些什么来绕过CORS限制,有什么想法吗?

EDIT: 编辑:

Headers: 标头: 在此处输入图片说明

Cors config: 核心配置:

return [
    /*
    |--------------------------------------------------------------------------
    | Laravel CORS
    |--------------------------------------------------------------------------
    |
    | allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
    | to accept any value.
    |
    */
    'supportsCredentials' => false,
    'allowedOrigins' => ['*'],
    //Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Cache-Control
    'allowedHeaders' => ['*'],
    'allowedMethods' => ['*'], // ex: ['GET', 'POST', 'PUT',  'DELETE']
    'exposedHeaders' => [],
    'maxAge' => 0,
];

You have two options, one is making your application to understand cors, and the second is to disable cors in the fetch options in your apollo client: 您有两种选择,一种是使您的应用程序了解cors,第二种是在apollo客户端的fetch选项中禁用cors:

const link  = new HttpLink({
        uri: 'URI',
        fetchOptions: {
            mode: 'no-cors'
        }
    });

暂无
暂无

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

相关问题 已被 CORS 政策阻止:无“访问控制允许来源” - has been blocked by CORS policy: No 'Access-Control-Allow-Origin' PHP-CORS策略已阻止对字体的访问:没有“ Access-Control-Allow-Origin”标头 - PHP - Access to Font has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header 访问 abcd.com 上的字体已被 CORS 政策阻止:没有“访问控制允许来源” - Access to Font at abcd.com has been blocked by CORS policy: No 'Access-Control-Allow-Origin' CORS问题-“所请求的资源上没有'Access-Control-Allow-Origin'标头。” - CORS-issue - “No 'Access-Control-Allow-Origin' header is present on the requested resource.” 错误:请求的资源上没有“Access-Control-Allow-Origin”标头 - Error: No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - No ‘Access-Control-Allow-Origin’ header is present on the requested resource PHP:请求的资源上不存在“Access-Control-Allow-Origin”标头 - PHP : No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源 Symfony 上不存在“访问控制允许来源”header - No 'Access-Control-Allow-Origin' header is present on the requested resource Symfony
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM