How to fetch data from localhost using PHP and React JS | How to display json data in react js
11.9 هزار بار بازدید -
4 سال پیش
-
How to fetch data from
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
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
4 سال پیش
در تاریخ 1399/10/21 منتشر شده
است.
11,918
بـار بازدید شده