0

i just learn about Ajax. I am so confused and getting error after following some tutorials. The plan is after we get the id_program, we can change or reload the page to show the data inside the dropdown menu after we clicks it. May someone help me please?

1.1 Database ( Program )

enter image description here

1.2 Inside Payment

enter image description here

1.3 Inside actor enter image description here


  1. DatabaseController.php

    public function data($id)
     {
         $client = new Client();
         $response = $client->request('GET', 'http://localhost:8585/api/v1/tables', [
             'query' => [
                 'limit' => '100'
             ]
         ]);
         $data = json_decode($response->getBody()->getContents(), true)['data'];
         $pay = Payment::get()->all();
         $pro = Program::select('nama')->where('name', $id)->get();
         $coba = Program::get()->all();
         foreach ($pro as $p) {
             $name = $p->name;
         }
         return view('database.index', compact('nama','data','pay','coba','pro','id'));
     }
    

  1. Route

    Route::get('program/database/{database}', [DatabaseController::class,'data'])->name('database.data');
    

  1. database.index

    <select id="id" class="form-control" filter>
            <option>Pilih Layanan...</option>
                 @foreach ($coba as $id)
                      <option value="{{$id->id_program}}">{{ $id->nama}}</option>
                 @endforeach
    </select>
    

  1. Ajax Script

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

         $(function(){
             $('#id').on('change',function(){
                number =$('#id').val();
    
                 console.log(number)
    
                 $.ajax({
                     type : 'POST',
                     url : "{{route('database.data',lcfirst($name))}}",
                     cache : false,
                     data:function(d){
                         d.number = number; 
                         return d;
                     },
    
                     success: function(msg){
                         console.log('success')
    
                     },
                 })
             })
         })
     });
    

  1. Result (Error)

a. ( id_program = 1 ) Correct data output & Method Not Allowed error

enter image description here

b. ( id_program = 2 ) Incorrect data output & Method Not Allowed error


enter image description here


Thank you for the helps

4
  • 1
    you are doing a POST request on your ajax call, while you are declaring the route as Get Commented Oct 26, 2022 at 9:28
  • @didenetahi from some tutorials i follows, it set so. And after i try to change it to "GET", i got another error. Commented Oct 26, 2022 at 9:34
  • what errors you having after setting GET? Commented Oct 26, 2022 at 9:36
  • @didenetahi sorry, Sir. My bad. Now it fixed. I don't know how, i was just stressed out and clear cache laravel and close the browser. || But, i really not lying if i was getting error when i change it to "GET". I have been stucking in it for 1-2 hours :'D Commented Oct 26, 2022 at 9:40

1 Answer 1

2
Route::get('program/database/{database}', [DatabaseController::class,'data'])->name('database.data');

see the "get" key word? You are declare this route as a GET route, but your AJAX method:

 $.ajax({
     type : 'POST',
     url : "{{route('database.data',lcfirst($name))}}",
     ....

is a POST request.

The solution is:

  • if your request only GET some data (no create, no delete, no update,... any data), then $.ajax({ type : 'GET', ....

  • if your request do modify data, then Route::post('program....

Sign up to request clarification or add additional context in comments.

Comments

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.