A
A
Alex_Cross2019-04-22 17:49:19
Laravel
Alex_Cross, 2019-04-22 17:49:19

React + Laravel + Axios. axios.post() not working?

axios.post() not working gives POST 127.0.0.1:8000/work/store 500 (Internal Server Error) error.
I tested, for some reason the program does not enter axios and passes by as soon as it sees axios.post(' 127.0.0.1:8000/work/store ', category).
My js:

import React, { Component } from 'react';
import axios from 'axios';

export default class Add extends Component {
  constructor(){
    super();
    this.onChangeCategoryName = this.onChangeCategoryName.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
    this.state = {
      category_name: ''
    }
  }
  onChangeCategoryName(e){
    this.setState({
      category_name: e.target.value
    });
  }
  onSubmit(e){
    e.preventDefault();
    const category = {
      category_name : this.state.category_name
    }
    axios.post('http://127.0.0.1:8000/work/store', category)
    .then((response) => {
  console.log(response.data);
  });
  }
    render() {
        return (
          <div>
              <form onSubmit={this.onSubmit}>
                   <div className="form-group">
                     <label for="exampleInputEmail1">Category Name</label>
                     <input type="text"
                     className="form-control"
                     id="exampleInputEmail1"
                     value={this.state.category_name}
                     onChange={this.onChangeCategoryName}
                     placeholder="Enter catagory" />
                   </div>
                   <button type="submit" className="btn btn-primary">Submit</button>
              </form>
          </div>
        );
    }
}

My controller
public function store(Request $request)
    {
      $category = new Category();
      $category->text = $request->category_name;
      $category->save();
    }

My Route in Web.php
Route::post('work/store', 'Api\[email protected]');

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question