0

Created a route for inserting todo in lumen , its working perfectly using postman but in my react application request sending with axios , it getting error

this.apiUrl = 'http://lumenback.dev/createTodo';

axios.post(this.apiUrl, {
  todo: this.state.todo,
  todo_date: this.props.curDate
})
.then(function (response) {
  console.log(response);
}).catch(function (error) {
  console.log(error);
});

enter image description here

thanks in advance...

Rathilesh C
  • 131
  • 2
  • 14

2 Answers2

2

Your application is not accepting the Cross domain requests I guess.

Here is an answer Lumen API CORS Ajax 405 Method Not Allowed I wrote to setup Cors and make it working with React and Lumen 5.5.

See if this can help.

I cannot comment so writing this solution here.

Puneet
  • 468
  • 9
  • 17
  • after adding belowe code fixedmy problem Route::options( '/{any:.*}', [ 'middleware' => ['CorsMiddleware'], function (){ return response(['status' => 'success']); } ] ); – Rathilesh C Oct 31 '17 at 16:57
0

This is a stab in the dark, but have you tried setting the headers on axios first?

Insert this before your post command: axios.defaults.headers.post["Content-Type"] = "application/json";

greendemiurge
  • 509
  • 3
  • 11
  • no luck , still getting error Response to preflight request doesn't pass access control check: 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 405 – Rathilesh C Oct 30 '17 at 16:09
  • Then the error is most likely being thrown by Laravel due to the route configuration. Heave you checked this post: https://stackoverflow.com/questions/31677198/laravel-5-method-not-allowed-http-exception-in-route-collection-php-line-201 ? Specifically, have you perhaps configured the route for a GET but not a POST? – greendemiurge Oct 30 '17 at 16:11
  • i think it is related the cors – Rathilesh C Oct 30 '17 at 16:12
  • but i added the core middleware too that why my get request is working, but not in post request – Rathilesh C Oct 30 '17 at 16:12
  • What happens if you try using CURL to test your endpoint directly: curl -X POST http://lumenback.dev/createTodo -d '{todo: dummyData, todo_date: dummyData}' Do you get an error? – greendemiurge Oct 30 '17 at 18:17
  • hello whats the solution on you? i got this too with axios – mandaputtra Feb 20 '19 at 03:46