0

I need to pass only one value from view to controller,using Ajax(in View home.blade.php). All solutions are for Laravel 5's and it's not helpful.

Ajax from home.blade.php:

$datee="hello";

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

 $.ajax({
    type        : 'POST',
    url         : "insert2",
    contentType: "text",
    data:      datee ,
    dataType: 'json'
  });

Route:

Route::post('/insert2/{datee}', 'EditContdroller@show1');
Route::get('/home', 'HomeController@index')->name('home');

And EditController's method:

  public function show1(Request $request){

                         $data= $request->datee;
//some work with $data...
                         return $json_Value;

    }

I get Error 404 Post .../insert2 not found.Have you any idea,please?

4
  • Since you are sending the data inside the request, you should remove {datee} parameter from your laravel route. Also add a slash before the url in your ajax call url: "/insert2", and send the data as object data: {datee: datee},. Commented May 6, 2020 at 15:36
  • thanks, but now I get POST .../insert2 500 (Internal Server Error). Maybe it's because of $.ajaxSetup ? Commented May 6, 2020 at 16:03
  • You should look in the laravel logs when you get a response with status 500. But I also tell you some other considerations. Add X-Requested-With header to ajaxSetup $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') , 'X-Requested-With': 'XMLHttpRequest', } }); .Add the csrf token to the data too data: {datee: datee, _token: $('meta[name="csrf-token"]').attr('content') } Commented May 6, 2020 at 16:10
  • Thanks,I solved it with logs. Commented May 6, 2020 at 17:08

1 Answer 1

1
Route::post('/insert2/{datee}', 'EditContdroller@show1');

url:"insert2",

Your post route requires additional parameter, but you request without parameter

It should be url: "insert2/something"

Now in Controller the "something" you passed will become variable {datee}

If you want to make the parameter optional, you should add question mark to {datee} making it {datee?} then your AJAX request should work: (I added '?' question mark to the date)

Route::post('/insert2/{datee?}', 'EditContdroller@show1');

You are passing:

data: datee,

It doesn't work like this.

To pass the datee, I would recommend doing so:

Remove the {datee} part

Route::post('/insert2', 'EditContdroller@show1');

In your AJAX request modify your data like this:

data: { datee: datee },

In your Controller access datee value like this:

$datee = $request->input('datee');
Sign up to request clarification or add additional context in comments.

2 Comments

tomJo thanks, but now I get POST .../insert2 500 (Internal Server Error). Maybe it's because of $.ajaxSetup ?
500 is a Server Error. You may have some mistakes in PHP code you wrote or similar. AJAX is probably not causing this problem. If I were you, I would debug the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.