Problem with routes in the Axios + Aravel

Asked

Viewed 215 times

-1

I’m using vue.js with axios to make Cruds but I have a question about routes.

I have the following routes:

| DELETE   | admin/deleteprod/{id}    | componet_3.produtos | App\Http\Controllers\ProdutoController@destroy                                   
| PUT      | admin/updateprod/{id}    | componet_2.produtos | App\Http\Controllers\ProdutoController@update 

I’m using the following methods:

  updat:function(id){
      var url='updateprod/'+ id;  
     axios.put(url,  this.fillproduto).then(response=>{

        this.getProdutos();
        this.fillproduto={'id':'','nome_pro':'','descricao_pro':'' };
        $('#edit').modal('toggle');

    });
    console.log(url);

    }, 

   deleteprod:function(id){
    var url='deleteprod/'+ id;
    axios.delete(url).then(response=>{
        this.getProdutos(); 
    });
    //alert('id'); 
    console.log(url); 
  } ,

As you can see, I’m not using the url in the methods only the last parameter, because if I use the url whole, the method stops working.

My question is whether this is normal and can generate any future problems in my application. I haven’t found any documentation about this.

  • 1

    Yes. You may have problems. If you have at the beginning page (/), the request will be made to https://www.example.com/updateprod/<id>, but if you have in a sub-page (/category/t-shirt), the request will be made to https://www.example.com/category/t-shirt/updateprod/<id>. P.S.: Unless you set the option baseURL

1 answer

1


Two tips:

  1. I recommend you define the baseUrl in Xios and if you need to access another url, just use the absolute path /o-path or a new url http://..../o-path.

You can use Axios.create() for this.

  1. No need to add in the path "update" or "delete" ... The verb (PUT/DELETE/...) already serves for this purpose.

Your endpoint would be PUT admin/{id}. The PUT verb, you already know it’s an update.

Axios Settings: - https://github.com/axios/axios#request-config

  • Thank you so much for the tip Eduardo Stuart!!!

Browser other questions tagged

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