展开时未出现Flask闪光

2024-05-19 20:12:20 发布

您现在位置:Python中文网/ 问答频道 /正文

@app.route("/communicate", methods = ["POST"])
def communicate():
  data = request.get_json()
  for i in data:
    flash(i)
    
  print(data)
  return "Hello"

我有一个/communicate链接,它从一个事件侦听器获取json数据

if (messages.length > 0) {
    e.preventDefault()
    let data = JSON.stringify(messages)
    
    fetch(`${window.origin}/communicate`, {
    method: "POST",
    credentials: "include",
    body: JSON.stringify(messages),
    cache: "no-cache",
    headers: new Headers({
      "content-type": "application/json"
    })
  })
    location.reload()
  }

当我在本地计算机上测试完全相同的代码时,for循环中的flash正在工作。然而,当我在heroku和Python上部署它时,它停止了工作

注意:部署时的/communicate路由仍然可以很好地从js获取数据,只是每个被窃听的数据内容都在闪烁

我尝试了以下方法:

  • 正在删除位置。重新加载()
  • 将get_flashed_messages()更改为特定的html文件,而不是在base.html上

旁注:我使用location.reload()刷新页面并加载/更新刷新的消息

谢谢~


Tags: 数据jsoncachefordataget部署location
1条回答
网友
1楼 · 发布于 2024-05-19 20:12:20

我发现使用flash()之类的东西会比使用普通javascript和空的小标记给我带来更大的伤害

const name = document.getElementById('username')
const password = document.getElementById('password')
const form = document.getElementById('mainform')
const second_password = document.getElementById("secure_pass")


const password_error = document.getElementById("password-error")
const second_error = document.getElementById("second-error")
const name_error = document.getElementById("name-error")

form.addEventListener('submit', (e) => {
  let messages = []
  if (name.value === '' || name.value == null) {
    messages.push('Filler')
    name_error.innerText = "Name is required."
  }
  else{
    name_error.innerText = ""
  }

  
  
  
  
  if (password.value != second_password.value || second_password.value != password.value){
    
    messages.push("Filler")
    second_error.innerText = "Password doesn't match"
  }
  else{
    second_error.innerText = ""
  }
  

  if (password.value.length >= 20) {
    messages.push('Filler')
    password_error.innerText = "Password should be less than 20 characters"
  }
  else if (password.value.length <= 8){
    messages.push('Filler')
    password_error.innerText = "Password should be atleast 8 characters"
  }
  else{
    password_error.innerText = ""
  }

  if (password.value === 'password') {
    messages.push('Password cannot be password \n')
  }

  if (messages.length > 0) {
    e.preventDefault()    
  }
 
})

请不要介意“填充”我只是添加了一个计数器来防止提交

let count = 0 
count += 1 

更简单

相关问题 更多 >