How to fetch data from localhost using PHP and React JS | How to display json data in react js

Опубликовано: 10 Январь 2021
на канале: Dev Amit Jha
12,199
183

How to fetch data from localhost using PHP and React JS | How to make API using PHP and my sql | how to display json data in react js

use below command for installing and creating react app:
-- first install nodejs
-- npm install react --save
-- npm install react-dom --save
-- npx create-react-app my-app
-- cd my-app
-- npm start

PHP core:
/****Simply add this code in you .php file and call it when required****/
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
}
/************************/
Music: "Royalty free music from Bensound"

You can donate at : paypal.me/devamitjha

Freelancing for your website, comment below or contact me

Subscribe and Comment below for source code

#devamitjha #API #reactjs #fetchAPI #PHP