This may seem duplicate but I have this problem using AJAX and I don’t know how to fix it.
Routes: web.php
Route::get('/{name}/{code}', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="76221305023519180204191a1a13043615190318021304">[email protected]</a>'); Route::post('/{name}/{code}', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a0e3f292e1935342e283536363f281a293f343e3f28">[email protected]</a>')->name('sender');
Controller: TestController.php
public function counter() { return view('room'); } public function sender(Request $request) { $xd = array( '1' => request('text'), '2' => Request::url() ); event(new TestEvent($xd)); }
View: room.blade.php:
<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <div> <form> @csrf <label for="lname">Code:</label><br> <input type="text" id="text" name="text"><br><br> <input type="submit" value="Submit"> </form> </div> <script type="text/javascript"> $("form").on("submit", function (e) { var data = $(this).serialize(); $.ajax({ type: "POST", URL: "{{ route('sender') }}", data: data, success: function () { console.log("works"); } }); e.preventDefault(); }); </script> </body> </html>
The error is the AJAX if I take it out I don’t get an error. The AJAX code is to call the Controller without redirecting to another page. Any idea how to fix it?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Use This Route:
Routes: web.php
Route::get('', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="16427365625579786264797a7a73645675796378627364">[email protected]</a>'); Route::post('', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b7f4e585f6844455f594447474e596b584e454f4e59">[email protected]</a>')->name('sender');
If you want to send a parameter, then define your parameter in your route, like below:
Routes: web.php
Route::get('/{name}/{code}', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1541706661567a7b61677a7979706755767a607b617067">[email protected]</a>'); Route::post('/{name}/{code}', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="efbb8a9c9bac80819b9d8083838a9daf9c8a818b8a9d">[email protected]</a>')->name('sender');
Controller: TestController.php
public function counter($name, $code) { //---Note: Use $name and $code in your method return view('room', compact('name', $code)); } public function sender(Request $request, $name, $code) { //---Note: Use Those parameters in your method $xd = array( '1' => request('text'), '2' => Request::url() ); event(new TestEvent($xd)); }
View: room.blade.php:
<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <div> <form> @csrf <label for="lname">Code:</label><br> <input type="text" id="text" name="text"> <br /> <br /> <input type="submit" value="Submit"> </form> </div> <script type="text/javascript"> $("form").on("submit", function (e) { var data = $(this).serialize(); $.ajax({ type: "POST", URL: "{{ route('sender', ['name' => '', 'code' => '']) }}", data: data, success: function () { console.log("works"); } }); e.preventDefault(); }); </script> </body> </html>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0