代码之家  ›  专栏  ›  技术社区  ›  ravenUSMC

AXIOS响应是一个字符串,其中包含回车符,应该是一个数组

  •  0
  • ravenUSMC  · 技术社区  · 7 年前

    我正在使用python和vue.js开发一个小项目。在这个项目中,我提出了一个AXIOS请求,它应该是一个数组,但返回的是一个字符串。从我的响应来看,字符串如下:

    但首先,我看到了这样的反应: Axios Get request data comes back with "data: ↵ ↵ ↵ ↵" 就像是我的问题。然而,正如我稍后在这个问题中所说的,我刚刚完成了一个类似的项目,AXIOS工作得很好!代码几乎是相同的。我把代码粘贴到这个项目中,它就工作了!

    "[↵  [↵    "United States", ↵    86.19↵  ], ↵  [↵  …, ↵    0↵  ], ↵  [↵    "US-France", ↵    0↵  ]↵]↵"
    

    不应该是这样。它应该是一个数组。以下是我的AXIOS请求:

    const path = 'http://localhost:5000/worldMapData';
    axios.post(path, varietyObject)
    .then((res) => {
      console.log(res)
      console.log(typeof res.data)
      commit('setWineData', res.data)
    })
    .catch((error) => {
      console.log(error);
    });
    

    这就是python路由的样子:

    @app.route('/worldMapData', methods=['GET', 'POST'])
    def route_seven():
    map = Map()
    if request.method == 'POST':
        post_data = request.get_json()
        variety = post_data.get('variety')
        wineData = map.get_wine_data(variety)
        print(wineData)
        return jsonify(wineData)
    

    现在最奇怪的事情,如前面提到的,是我刚刚完成了一个以前的项目,做了一个类似的事情,没有问题。我甚至把代码粘贴到我当前的项目中,它工作得很好!我真的很好奇,在我的回答中,车厢的返回是从哪里来的。这是一些更多的AXIOS响应:

       headers: {content-type: "application/json"}
       request: XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
       status: 200
       statusText: "OK"
    

    如果您需要在此处查看请求所在的python代码:

      def get_wine_data(self, variety):
        # print(self.data.dtypes)
        #This list will hold all of the country and wine scores
        wine_data = []
        #Getting all distinct countries
        countries = self.data.country.unique()
        for country in countries:
            #resetting the data for each loop
            data = self.data
            #This list will hold the data for a single country
            single_country = []
            data = data[(data.country == country) & (data.variety == variety)]
            #Getting the mean score.
            mean = data["points"].mean()
            #changing the format of the mean
            mean = float(format(mean, '.2f'))
            if math.isnan(mean):
                mean = 0
            if country == 'US':
                country = 'United States'
            single_country.append(country)
            single_country.append(mean)
            wine_data.append(single_country)
        return wine_data
    

    顺便说一下,这是对原始项目的响应,也是其中的一部分:

    data: Array(141), status: 200, statusText: "OK"
    

    这就是我在当前项目中要看到的:data:array(141),最后,在我离开python和jsonify之前,我将信息发送到它的数据类型是一个列表。如有任何帮助,我们将不胜感激。

    1 回复  |  直到 7 年前
        1
  •  0
  •   ravenUSMC    7 年前

    好吧,我相信AXIOS代码没问题。我用的是csv文件。不需要JSONP。