在Vuejs中编码Url时出现问题

时间:2018-04-24 07:34:17

标签: javascript vue.js vuejs2

我正在使用axios包对url进行编码并发出get请求。 url的一些参数之间有空格然后我在发送请求之前使用encodeURI函数对url进行编码,但仍未正确发送请求。

这是我在Vuejs中的组件代码

<template>
</template>

<script>

import axios from 'axios'
name:  'GetCategoryItemsAndDisplayOne',
data(){
    return {
        catalogselected : "macy's",
        data_to_show_on_mainpage : [],
        categoriesfordisplay :{men_for_display:[],women_for_display:
        []}

    }
},
methods:{
  CatlogService(catlog_name,category,gender,mainpage){


        let url = "http://localhost:5000/xyz/" + (this.catalogselected) +"/api/"+ (gender) + "/catalogvis/" + (category) +"/items"


        axios.get(encodeURI(url)).then((resp)=>{

            try{
                this.data_to_show_on_mainpage.push(resp.data.response.Results.results[0])
                console.log(this.data_to_show_on_mainpage)
            }
            catch(e){
                console.log("we got an error the url is " + url)

            }


        })
    },
  GetItemsToShowonMainPage(){
       this.categoriesfordisplay.men_for_display = ["cat and dog", "coats and jackets", "good and bad","bad day"]

       this.categoriesfordisplay.women_for_display = ["slow poke", "coats and jackets", "good","mad world","bathroom wear"]

        if(this.categoriesfordisplay.men_for_display.length>0){
            var i=0;
            for(i =0;i<this.categoriesfordisplay.men_for_display.length;i++){
                let category = this.categoriesfordisplay.men_for_display[i].replace(/"/g,""); 

                this.CatlogService(this.catalogselected,category,'men',this.ismainpage)

            }

        }
        if(this.categoriesfordisplay.women_for_display.length>0){
            var i=0;
            for(i =0;i<this.categoriesfordisplay.women_for_display.length;i++){
                let category = this.categoriesfordisplay.women_for_display[i].replace(/"/g,"");

                this.CatlogService(this.catalogselected,category,'women',this.ismainpage)

            }

        }




    },
  created(){
  this.GetItemsToShowonMainPage()
 }
</script>
<style></style>

当我以这种方式提出请求时,Url编码不正确。 这是一个例子 http://localhost:5000/xyz/macy's / api / women / catalogvis / bathroom wear / items

我甚至尝试过encodeURIComponent()但仍然无法正常工作。 我哪里错了? 如何解决这个问题。

0 个答案:

没有答案