call authenticated Laravel api

Asked

Viewed 266 times

-1

Good morning, all right? I am reading an api (which can only be accessed with user authentication) done in the Standard. To consume this data, I am using jquery/ajax. I know I need to send a token to the api in order to access it and for that, I am using the following code :

$.ajaxSetup({
  headers: { 'X-CSRF-TOKEN': TokemMovies },
  method:'GET',  
  url:'http://localhost/testeEmprego/public/api/filmes',
  success:function(response){
    console.log(response.length);
    for(var i = 0; i< response.length; i++) 
           { 
               console.log(''+response[i].nome);
               $('.filmes').prepend(
                   "<li>"
                   +"<h3>"+response[i].nome+"</h3>"
                   +""+response[i].ano+""
                   +"<p>"+response[i].sinopse+"</p>"
                   +"<div><img src='"+endereco+""+response[i].imagem+"' alt=''></div>"
                   +"<div class='select'>"
                        +"<form class='likex'>"
                            +"<input type='hidden' class='filmeCod' name='filmeCod' value='"+response[i].id+"'>"
                            +"<label for='gostei_"+i+"'>gostei </label>"
                                +"<input id='gostei_"+i+"'    class='like' type='radio' value='1' name='like'>"
                            +"<label for='naoGostei_"+i+"'>não gostei</label>"
                                +"<input id='naoGostei_"+i+"' class='like' type='radio' value='0' name='like'>"
                        +"</form>"
                   +"</div>"
                   +"</li>"
                   );
           }
  },

however, I can’t read anything from the api, I mean, I must not be sending the token correctly. Anyway, what’s wrong here?? Thank you all for your attention! Horatio

  • Confirm in your request header that the token is being sent correctly, if possible test with the same token using Postman.

  • then, but in this case, the "X-CSRF-TOKEN" is the correct name of the variable that should be sent to the api? or for authenticated api routes there is another name? which bothers me more, is with which variable name I send this token...

  • This information will depend on your API, for example the`s API I usually do I use JWT, and the parameter using the header is Bearer.

  • i used the Passport...

  • and whenever I try to access it, it appears on the console: "Failed to load Resource: the server responded with a status of 401 (Unauthorized)"

1 answer

0

I was with this problem a few days ago I solved as follows...

url:'http://localhost/testeEmprego/public/api/filmes?api_token='+Token_api

The variable Token_Api step into her token via front-end and call her in js when you need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.