I am new in React.I am looking for how to save the data of a form with axios in a database. But not working..
My API.JS
const axios = require ("axios");
const querystring = require('querystring');
export function getPeople(){
return axios.get(`http://127.0.0.1:9000/people`)
}
export function postPeople(){
axios.post('http://127.0.0.1:9000/people', querystring.stringify({
'bar': 123
}));
}
My app.js:
import React, { Component } from 'react';
import { getPeople, postPeople } from './api';
addItem = () => {
postPeople();
}
My Express.js:
var express = require('express')
var cors = require('cors')
var app = express()
app.get('/people', cors(), function (req, res, next) {
res.json([
{
id: 0,
name: "0",
age: 20,
city: 'R0eiro',
country: '04'
},
{
id: 1,
name: "0",
age: 29,
city: 'Minas 00',
country: '00'
},
})
app.listen(9000, function () {
console.log('The server is running in the port 9000')
})
Be givin that errors:
POST http://127.0.0.1:9000/people 404 (Not Found)
Failed to load http://127.0.0.1:9000/people: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 404.
Uncaught (in promise) Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:87)
Someone help me?
Get
is working.. – Jota