【问题标题】:Axios response is a string, with carriage returns in it, should be an arrayaxios响应是一个字符串,里面有回车,应该是一个数组
【发布时间】:2023-04-07 23:19:02
【问题描述】:

我正在做一个使用 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 之前,我将信息发送到它的数据类型是一个列表。对此问题的任何帮助将不胜感激。

【问题讨论】:

    标签: python pandas vue.js axios


    【解决方案1】:

    好的,我相信 axios 代码没问题。这与我使用的 CSV 文件有关。不需要 JSONP。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多