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

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?

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

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.


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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x