Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am new in laravel. I want to send a parameter from view to a function in controller. then, that function processes the parameter and sends back the result to the view. the function in controller is the same as below:

public function ajax(Request $request)
   {
       $fullName = $request->get('fullName');

       return response()->json(['msg' => 'Hello ' . $fullName], 200);
   }


also, the route is as the following:
Route::post('/{fullname}', 'App\Http\Controllers\marketerController@ajax');

and the ajax is as follows:
$.ajaxSetup({

               headers: {

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

               }
           });
           var fullName ="fullname";
                   $.ajax({
                       type: 'post',
                       data: {
                           'fullName': fullName
                       },
                       url: "{{url('/fullname')}}",
                       success: function(data) {

                           console.log(data);
                       },
                       error: function(data){
                           var errors = data.responseJSON;
                           console.log(errors);
                       }
                   });


In address localhost:8000 I get "500 internal Server Error". I do not know the reason. can you help me?

What I have tried:

I searched a lot in the internet, but I could not solved the problem.
Posted
Updated 30-Nov-22 22:21pm
Comments
Sandeep Mewara 30-Nov-22 12:34pm    
Did you add csrf token in head of your html layout like below?

<meta name="csrf-token" content="{{ csrf_token() }}">
Member 11807654 1-Dec-22 3:15am    
yes. I added it. I do not know why, but now it gets this error: "The POST method is not supported for this route. Supported methods: GET, HEAD." when I change the method in ajax to "get", it does not send the parameter to controller!!! I have a select box in a form with post method and I wrote the ajax code for it. is the error related to the form? when I change the value of select box, this error occurs.

1 solution

Quote:
The POST method is not supported for this route. Supported methods: GET, HEAD.
That error means the endpoint you're calling doesn't support POST requests.

Quote:
when I change the method in ajax to "get", it does not send the parameter to controller
Whilst there's technically nothing in RFC 7231 §4.3.1[^] that forbids a GET request from having a body, it's virtually unheard of for any server to support it. Parameters to GET requests need to be passed in the URL or query-string.

I'm not familiar with Laravel, but it looks like your server-side method expects the parameter to be passed in the querystring.
JavaScript
$.ajax({
   type: 'get',
   url: "{{url('/fullname')}}?fullName=" + encodeURIComponent(fullName),
   success: function(data) {
       console.log(data);
   },
   error: function(data){
       var errors = data.responseJSON;
       console.log(errors);
   }
});
 
Share this answer
 
Comments
Member 11807654 3-Dec-22 7:22am    
thanks a lot. it worked for me :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900