mardi 7 septembre 2021

Problem with javascript and laravel - Save token

i am working with push notifications, i need to save the token it generates and pass it through POST

Javascript:

 function pedirPermiso(){
    messaging.requestPermission()
    .then(function(){
        console.log("Se han haceptado las notificaciones");
        hideAlert();
        return messaging.getToken();
    }).then(function(token){
        console.log(token);
        guardarToken(token);
    }).catch(function(err){
        console.log('No se ha recibido el permiso');
        showAlert();
    });
}
function guardarToken(token){
    var formData=new FormData();
    formData.append('token',token);
    axios.post('/token',formData).then( respuesta=>{
        console.log(respuesta);
    }).catch( e=>{
        console.log(e);
    });
}

Route:

Route::post('/token', 'savetoken@HomeController' )->name('token');

Controller:

     public function savetoken()
{
        Notificaciones::insert([
          'user_id'  => 1,
          'token'        => ("token")
        ]);
  
        return back()->with('success', 'token saved successfully!');
}

Error in console: https://i.imgur.com/BfH0Onk.png

how to save the token correctly?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2X24NYP
via IFTTT

Aucun commentaire:

Enregistrer un commentaire