samedi 22 février 2020

Request failed when using Axios in ReactJs and Laravel

I using ReactJs as my front-end engine and I handle a request using Axios to get data from my Post table by Laravel I set route in api.php when I get a response and console.log it it shows Access to XMLHttpRequest at error at my chrome DevTool

api.php file:

<?php

use Illuminate\Http\Request;

Route::get('/', [
    'uses' => 'PagesController@getIndex'
]);

my controller file:

<?php

namespace App\Http\Controllers;

use App\Posts;
use Illuminate\Http\Request;

class PagesController extends Controller
{
    public function getIndex(){
        $post = Posts::all();

        return $post;
    }
}

my ReactJs file(App.js):

import React from 'react';
import logo from './logo.svg';
import './App.css';
import axios from 'axios';

class App extends React.Component{

    getData(){
        console.log(123);
        axios.get('http://localhost:8000/')
            .then(function (response) {
                console.log(response.data);
            }).catch(function (error) {
            // handle error
            console.log(error);
        });
    }

    render(){
        return (
            <div className="App">
                <header className="App-header">
                    <img src={logo} className="App-logo" alt="logo" />
                    <p>
                        Edit <code>src/App.js</code> and save to amir.
                    </p>
                    { this.getData() }
                    <a
                        className="App-link"
                        href="https://reactjs.org"
                        target="_blank"
                        rel="noopener noreferrer"
                    >
                        Learn React
                    </a>
                </header>
            </div>
        );
    }
}

export default App;

Error screenshot:

enter image description here



via Chebli Mohamed

1 commentaire:

  1. Strange "water hack" burns 2lbs overnight

    Over 160,000 men and women are hacking their diet with a easy and SECRET "liquid hack" to burn 2 lbs each and every night while they sleep.

    It is very simple and it works with everybody.

    Here's how you can do it yourself:

    1) Go get a glass and fill it half full

    2) And then do this weight loss hack

    and become 2 lbs skinnier when you wake up!

    RépondreSupprimer