Reactjs app data fetching from mysql problem

Tell us what’s happening:
error message in console as follows:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3003/posts. (Reason: CORS request did not succeed).

Your code so far

**reactjs code so far**
import React, { Component } from 'react';
// import ReactDOM from 'react-dom';

class Contact extends Component {
  constructor(props) {
    this.state = { posts: [] };

      .then(response => response.json())
      .then(posts => (this.setState({posts}))
  render() {
    return (<div>
      Hello World
        { => <li>

// ReactDOM.render(
//   <Contact />,
//   mountNode
// );
export default Contact


import React, { Component } from 'react';
import Contact from './Component/Contact';
import './App.css';

class App extends Component {
  render() {
    return (
      <div className="App">

export default App;


const express = require('express');
const bodyParser = require('body-parser');
const mysql      = require('mysql');
const connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'root',
  database : 'vanitha'

// Initialize the app
const app = express();

app.get('/posts', function (req, res) {

    connection.query('SELECT * FROM posts', function (error, results, fields) {
      if (error) throw error;

// Start the server
app.listen(3003, () => {
 console.log('Go to http://localhost:3003/posts to see posts');

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0.

1 Like

hi iam also suffering from same problem here

Reason: CORS request did not succeed
The HTTP request which makes use of CORS failed because the HTTP connection failed at either the network or protocol level. The error is not directly related to CORS, but is a fundamental network error of some kind.

Does going to http://localhost:3003/posts work properly? Do you have your MySQL server up and running locally? Tried writing/reading dummy data to/from it, to see if it works as intended?

Might be because CORS isn’t enabled on the server, this is due to security reasons by default, so no one else but the webserver itself can make requests to the server.

Add this middleware before your app.get call.

app.use(function(req, res, next) {
 res.header("Access-Control-Allow-Origin", "*");
 res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, 
 Content-Type, Accept");
1 Like