I'm working on a small project using python and Vue.js. Within this project I make an axios request which should be an array of array's but is returning a string. The string looks like this from my response:
But first, I did see this response: Axios Get request data comes back with "data: ↵ ↵ ↵ ↵" and was like that's my issue. However, as I will state later in this question, I just finished doing a similar project and axios worked fine! The code was almost identical. I pasted that code into this project and it worked!
"[↵ [↵ "United States", ↵ 86.19↵ ], ↵ [↵ …, ↵ 0↵ ], ↵ [↵ "US-France", ↵ 0↵ ]↵]↵"
It should not be this. It should be an array of array. Here is what my axios request looks like:
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);
});
This is what the python route look like:
@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)
Now the strangest thing, as mentioned earlier, is that I just finished a previous project doing a similar thing with no issues. I even took that code, pasted it into my current project, and it worked fine! I'm really curious as to where the carriage returns are coming from in my response. This is some more of the axios response:
headers: {content-type: "application/json"}
request: XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status: 200
statusText: "OK"
If you need to see the python code where the request is being made to its here:
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
By the way, this is the response, part of it, from the original project:
data: Array(141), status: 200, statusText: "OK"
This is what I want to see on my current project: data: Array(141) Finally, right before I leave Python and Jsonify the information before I send it over its data type is a list. Any help with this matter will be appreciated.