Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我能';t访问我的axios post请求中的数据_Javascript_Python_Python Requests_Axios - Fatal编程技术网

Javascript 我能';t访问我的axios post请求中的数据

Javascript 我能';t访问我的axios post请求中的数据,javascript,python,python-requests,axios,Javascript,Python,Python Requests,Axios,我试图访问我存储在localStorage中的数据,以便在Python路由中使用它 以下是我的axios请求: if ($('.box.genre').length === $('.box.genre.completed').length) { async function getScore() { let score = parseInt(localStorage.getItem("score")); const response = awa

我试图访问我存储在localStorage中的数据,以便在Python路由中使用它

以下是我的axios请求:

if ($('.box.genre').length === $('.box.genre.completed').length) {
    async function getScore() {
      let score = parseInt(localStorage.getItem("score"));
      const response = await axios.post(
        `${BASE_URL}/game-over`, { "score": score}, {
          headers: {
            'content-type': 'application/json'
          }
        }).then(response =>
          localStorage.getItem("score", response.data.score));
      console.log('Your score was: ', response);
     window.location = "/game-over";
    }
    getScore();  
  }
当我记录响应时,我可以看到分数。我的Python代码似乎不起作用。我无法得到数据。我得到了一个AttributeError:“非类型”对象没有属性“内容”

下面是我的Python:

@app.route('/game-over', methods=["GET", "POST"])
def game_over():

  if CURR_USER_KEY in session:
    user = User.query.get(session[CURR_USER_KEY])

  response = request.json()
  data = json.loads(response.content)
  score = data['score']

  user.high_score = score
  db.session.commit()

  high_score = user.high_score
  name = user.username

  return render_template('endgame.html', name=name, high_score=high_score)

我只是想从axios获得分数,以便更新我的SQL表。非常感谢你的帮助

代码中有两个问题

首先:如果要在本地存储器中设置项,必须使用
setItem
而不是
getItem

:使用异步函数时,不需要使用
。然后

async function getScore()
 const response = await axios.post(
  `${BASE_URL}/game-over`,
  { 'score': score },
  { headers: { 'content-type': 'application/json' }
 })

 if (response) {
  localStorage.setItem('score', response.data.score)
 }
}

我不明白你为什么使用
请求
。如果
axios
将带有
score
(JSON格式)的请求发送到
/game over
,则
game_over()
应使用
Flask.response.JSON()
获取请求。而
game\u over()
应该使用
return jsoinify({'score':score})
(而不是
return render\u template(…)
)将答案发送到
axios
(JSON格式)。Coderequests.get()不会将请求发送回web浏览器和axios,但会将请求发送到服务器上的其他URL,这意味着服务器上Flask中的其他函数。使用request.json()仍然会给我非类型错误。另外,如果我不呈现我的模板,我的页面将如何加载?我想在我的最后一页上显示分数和用户名。使用两个功能-一个在浏览器访问
http://example.com/game-over
第二步是使用
axios.post(“/game over”,…)
但它可能需要新的URL-即
/game over ajax
。或者在
def game_over()
中使用
if/else
检查它是正常请求还是ajax,并返回HTML(对于正常请求)或JSON数据(对于ajax请求),谢谢。过几个小时我回家后,我会试试这个。它不起作用。我已经在代码的前面将分数设置为本地存储。我正试图找回它。我认为问题出在我的Python代码中。我不知道使用哪种方法从axios响应中检索分数。