laravel + vue js API store function doesn't work on shared hosting but works well on localhost

Viewed 104

All cruds work well both on shared hosting and localhost except create (store).

here is my addtoqueue controller:

public function store(Request $request)
{
   $last = antri::whereDate('created_at', Carbon::today())->latest()->first();

   if ( $last ) {
       $tambah =  $last->number + 1;
   }
   else {
       $tambah =  1;
   }
    
   $newantri = new antri;
   $newantri->user_id = $request->user_id;
   $newantri->number = $tambah;
   $newantri->called = 0;

   $newantri->save();
   return $newantri;
}

model:

class antri extends Model
{
   use HasFactory;
}

api route :

Route::post('queue', [addtoqueue::class, 'store']);

the trigger:

<v-btn elevation="2" v-on:click="increase" >Ambil Nomor Antrian</v-btn>

the script:

methods: {
increase: function() {
    axios.post('/api/queue/', {
       user_id: this.user.id
    })
.  then( response=> {
     if( response.status == 200 ) {
      this.$emit('itemchanged');
     }
   })
.  catch( error => {
     console.log(error);
   })
  window.location.reload();      
}

tested using postman worked well too on both local and share hosting database. can anyone figure out what seems to be the problem?

1 Answers

I found the problem. I got Status Code 301 moved permanently every time post method is requested. this caused the url structure changed from domain/api/target to domain/public/api/targetand of course it won't hit the store function in the controller. I edited .htaccess file in public folder from: RewriteRule ^ %1 [L,R=301] to RewriteRule ^ %1 [L,R=307]

which means from permanently moved to temporarily redirected.

and this solved my problem.

Related