skip to Main Content

when i try to send a GET request with tutorial ,but its not working and end up with 500 internal server error
I cant find any error by searching on forum or stackoverflow,and also i ma beginner, kindly seek your help

here is my view ajax.blade.php

<html>
   <head>
      <title>Ajax Example</title>

      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>

      <script>
      $.ajax({
         type: "GET",
         url: "getmsg",
         success: function(data) {
             console.log(data);
         },
         error: function(data){
             console.log("fail");
         }
        });
      </script>
   </head>

   <body>
   </body>
</html>

here is the controller class

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

class aController extends Controller
{
    function index() {
      return response()->json(array('d'=> 'success'));
    }
}


here is web.php
Route::get('getmsg',[aController::class,'index']);

2

Answers


  1. Update your route with

    use AppHttpControllersaController;
    
    Route::get('/getmsg', [aController::class, 'index']);
    

    Laravel 8 requires you to provide a complete path to the controller or you can define a default namespace to locate your controllers like this, define following in your RouteServiceProvider.php

    protected $namespace = 'AppHttpControllers';
    

    You should get a response from the controller.

    Login or Signup to reply.
  2. Firstly check if you are using jquery-3.2.1.slim.min.js, slim version are not supported so remove slim version and replace it with:

    <script
      src="https://code.jquery.com/jquery-3.6.0.min.js"
      integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
      crossorigin="anonymous"
    ></script>
    

    then after if library link is correct check for things like csrf, route, data etc.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search