Привет всем, когда я пытаюсь отобразить продукты в базе данных. У меня проблема с получением данных:
app.js:285 GET http://127.0.0.1:8000/api/products 404 (Not Found)
dispatchXhrRequest @ app.js:285
xhrAdapter @ app.js:119
dispatchRequest @ app.js:765
Promise.then (async)
request @ app.js:542
Axios.<computed> @ app.js:557
wrap @ app.js:1131
componentDidMount @ app.js:74051
commitLifeCycles @ app.js:59612
commitLayoutEffects @ app.js:62601
callCallback @ app.js:39986
invokeGuardedCallbackDev @ app.js:40035
invokeGuardedCallback @ app.js:40090
commitRootImpl @ app.js:62339
unstable_runWithPriority @ app.js:72776
runWithPriority$1 @ app.js:50837
commitRoot @ app.js:62179
finishSyncRender @ app.js:61605
performSyncWorkOnRoot @ app.js:61591
(anonymous) @ app.js:50887
unstable_runWithPriority @ app.js:72776
runWithPriority$1 @ app.js:50837
flushSyncCallbackQueueImpl @ app.js:50882
flushSyncCallbackQueue @ app.js:50870
discreteUpdates$1 @ app.js:61691
discreteUpdates @ app.js:40604
dispatchDiscreteEvent @ app.js:43966
app.js:74056 Error: Request failed with status code 404
at createError (app.js:699)
at settle (app.js:960)
at XMLHttpRequest.handleLoad (app.js:168)
Это мои маршруты / API. php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
Route::resource('products', 'ProductController');
});
Это мой ProductController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::all();
return response()->json($products);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$product = new Product([
'title' => $request->get('title'),
'body' => $request->get('body'),
'price' => $request->get('price')
]);
$product->save();
return response()->json('Product Added Successfully.');
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$product = Product::find($id);
return response()->json($product);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$product = Product::find($id);
$product->title = $request->get('title');
$product->body = $request->get('body');
$product->price = $request->get('price');
$product->save();
return response()->json('Product Updated Successfully.');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$product = Product::find($id);
$product->delete();
return response()->json('Product Deleted Successfully.');
}
}
А вот DisplayProdcut. js Я пытаюсь показать его на веб-странице
import React, {Component} from 'react';
import axios from 'axios';
import { Link } from 'react-router';
import TableRow from './TableRow';
import MyGlobleSetting from './MyGlobleSetting';
class DisplayProduct extends Component {
constructor(props) {
super(props);
this.state = {value: '', products: ''};
}
componentDidMount(){
axios.get('/api/products')
.then(response => {
this.setState({ products: response.data });
})
.catch(function (error) {
console.log(error);
})
}
tabRow(){
if(this.state.products instanceof Array){
return this.state.products.map(function(object, i){
return ;
})
}
}
render(){
return (
<div>
<h1>Your pizza orders</h1>
<div className="row">
<div className="col-md-10"></div>
<div className="col-md-2">
<Link to="/add-item">Create Product</Link>
</div>
</div><br />
<table className="table table-hover">
<thead>
<tr>
<td>ID</td>
<td>Product Title</td>
<td>Product Body</td>
<td>Product Price</td>
<td width="200px">Actions</td>
</tr>
</thead>
<tbody>
{this.tabRow()}
</tbody>
</table>
<button className="btn btn-dark"><Link to="display-chosen-products">Add to Cart</Link></button>
</div>
)
}
}
export default DisplayProduct;
Вот как это выглядит chrome: веб-страница
Пожалуйста, я был бы признателен за любую помощь, предложения или идеи. Большое спасибо!
ОБНОВЛЕНИЕ: TableRow. js
import React, { Component } from 'react';
import { Link, browserHistory } from 'react-router';
import MyGlobleSetting from './MyGlobleSetting';
class TableRow extends Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
event.preventDefault();
let uri = MyGlobleSetting.url + `/api/products/${this.props.obj.id}`;
axios.delete(uri);
browserHistory.push('/display-item');
}
render() {
return (
<tr>
<td>
{this.props.obj.id}
</td>
<td>
{this.props.obj.title}
</td>
<td>
{this.props.obj.body}
</td>
<td>
{this.props.obj.price}
</td>
<td>
<form onSubmit={this.handleSubmit}>
<Link to={"edit/"+this.props.obj.id} className="btn btn-primary">Edit</Link>
<input type="submit" value="Delete" className="btn btn-danger"/>
</form>
</td>
</tr>
);
}
}
export default TableRow;
Отображение веб-страницыПродукт