2

Hello all here im doing practice of react. I created react app which has a search bar and clickable button. When i submit the data it can fetch users data from "https://www.drupal.org/search/user/" site. but here i can't fetch data from the site.

import React, { Component } from 'react'
import axios from 'axios';


class Search extends Component {

state = {
    name: ''

};

handleChange = event => {
    this.setState({ name: event.target.value });
}

handleSubmit = event => {

    alert("Hello Im : " + this.state.name);

    event.preventDefault();

    const user = {
        name: this.state.name
    };

    axios.post(`https://www.drupal.org/search/user/`, { user })
        .then(res => {
            console.log(res);
            console.log(res.data);
        })
    }

render() {

    return ( < >

        <div > < h1 > Search here < /h1>< /div >
        <form onSubmit = { this.handleSubmit } align = "center" >
        <input type = "search" name = "name" onChange = { this.handleChange } placeholder = "Search" required / >
        <button type = "submit" >Search < /button > 
        < /form >

        </>);
    }
}

export default Search;
  • what is the output of `console.log(res);` ? – Sakshi Jan 29 '21 at 14:33
  • Access to XMLHttpRequest at 'https://www.drupal.org/search/user/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. – Darshan Kalli Jan 29 '21 at 14:36
  • POST https://www.drupal.org/search/user/ net::ERR_FAILED – Darshan Kalli Jan 29 '21 at 14:37

1 Answers1

3

It's because of cross origin request blocked. You can easily bypass the CORS using this chrome extension Allow CORS: Access-Control-Allow-Origin

or you can manually do this via this disable-same-origin-policy-in-chrome

Thanks! :)