weblearner's avatar

Stack on query string with vuejs using AJAX

Hi, I am building an e-commerce site for my college graduation and facing an issue with vuejs to send the ajax call. URL like this is giving me result easly no what so ever issue here =>

https://ecommerce.test/api/categoryproducts?brand=apple

but now I want to send the request like this =>

https://ecommerce.test/api/categoryproducts?brand=apple&minPrice=0&maxPrice=6500

If i hit this URL I'm successfully fetching the result in JSON but now the issue is how can i send this request using vuejs component like in anchor tag.

Any way to solve this issue??

0 likes
4 replies
jeffreyvanrossum's avatar
Level 9

You could append parameters to the URL when the value of an input changes. Then after trigger a new request to fetch new results.

What have you got so far, in terms of code?

weblearner's avatar

Yes, I have tried that approach but it seems that it worked for only once but if I try to send the request again it will send the request like this

"/api/categoryproducts?minPrice=0&maxPrice=11500&brand=apple&brand=apple"

repeating the request again and again.

weblearner's avatar

Jeff, I came up with the way you have instructed and it seems to be working but still got a question when I append to the parameter at first it will send the request like this

https://ecommerce.test/api/categoryproducts?minPrice=0&maxPrice=9500&brand=

where the brand is empty and when I listen for the change event in the brand it will surely be like this

https://ecommerce.test/api/categoryproducts?minPrice=0&maxPrice=9500&brand=apple

But still, is it the best way to do it? OR is there any other best way to do this.

Thank you,

jeffreyvanrossum's avatar

You could probably check if the input has a value before you append it. That would prevent the empty parameters.

Not too long ago I made something familiar to this and I don't know if it is the best way to do it, but it works and I think it's a pretty good solution.

1 like

Please or to participate in this conversation.