Реагировать. js получение информации от API Drupal Rest с аутентификацией Cook ie - PullRequest
0 голосов
/ 03 марта 2020

У меня есть сайт на Drupal, который открывает меню с помощью https://www.drupal.org/project/rest_menu_tree

Я пытаюсь создать приложение React, которое импортирует канал API Rest из Drupal, использующего Cook ie Аутентификация. Но когда я пытаюсь импортировать канал с помощью Fetch, я получаю 403.

Для моего компонента реакции у меня есть:

import React, { Component } from 'react'
import MainMenuList from "../Menus/MainMenuList";

const LIST_URL = 'https://my-react.lndo.site/api/menu_items/main?_format=json';

export default class Header extends Component {

  constructor() {
    super();
    this.state = { data: null };
    this.loadMenuItems = this.loadMenuItems.bind(this);
    this.updateData = this.updateData.bind(this);
  }

  loadMenuItems() {
    // Fetch Menu.
    fetch(LIST_URL, {
      mode:'cors',
      credentials: 'include',
      headers: {
        'Accept': 'application/vnd.api+json',
        'Content-Type': 'application/vnd.api+json'
      }
    })
    .then(function (response) {
      return response.json();
    })
    .then((data) => this.updateData(data))
    .catch(err => console.log('Fetching Menu Items Faild.', err));
  }

И в файле Drupal service.yml у меня есть:

cors.config:
  enabled: true
  # Specify allowed headers, like 'x-allowed-header'.
  allowedHeaders: ['x-csrf-token','authorization','content-type','accept','origin','x-requested-with']
  # Specify allowed request methods, specify ['*'] to allow all possible ones.
  allowedMethods: ['*']
  # Configure requests allowed from specific origins.
  allowedOrigins: ['http://localhost:3000']
  # Sets the Access-Control-Expose-Headers header.
  exposedHeaders: true
  # Sets the Access-Control-Max-Age header.
  maxAge: 1000
  # Sets the Access-Control-Allow-Credentials header.
  supportsCredentials: true

Я что-то упустил?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...