Python 获取意外令牌<;在位置0的JSON中,即使im从flask后端返回JSON
我正在学习如何使用flask作为react应用程序的后端,我的flask后端只有一个路由,它以JSON返回当前时间。获取时对我的react应用程序进行响应,但我的开发控制台显示-Python 获取意外令牌<;在位置0的JSON中,即使im从flask后端返回JSON,python,json,reactjs,flask,fetch,Python,Json,Reactjs,Flask,Fetch,我正在学习如何使用flask作为react应用程序的后端,我的flask后端只有一个路由,它以JSON返回当前时间。获取时对我的react应用程序进行响应,但我的开发控制台显示- 意外标记
意外标记<在JSON中的位置0
我对此做了一些研究,结果表明,当您不返回json时,就会发生这种情况。但据我所知,它是自动的jsonifyies
dicts。所以请解释我哪里出了问题
这是我的react代码(仅相关部分,我没有包括jsx,因为它会使代码过长)-
所以我基本上在网页上按了一个提交按钮,这应该可以从我的烧瓶中获取数据并将其登录到控制台中
这是我的密码-
from flask import Flask
import time
app = Flask(__name__)
@app.route('/hola')
def home():
return {'time': time.time()}
任何帮助都将不胜感激,请提前感谢您需要在返回之前先查看字典 试一试
我知道这个问题的原因,但在jquery中,我也遇到了这个问题。原因是后端的数据类型是脚本,但作为响应,它需要JSON数据。当您获取或更新数据时,必须使用JSON.stringify(data)。您在位置0处获得错误
意外标记开头嘿,谢谢,但这似乎不起作用。我也犯了同样的错误
from flask import Flask
import time
app = Flask(__name__)
@app.route('/hola')
def home():
return {'time': time.time()}
from flask import jsonify
@app.route('/hola')
def home():
return jsonify({'time': time.time()})
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
@app.route('/hola')
def home():
try:
return {'time': time.time()}
except Exception as ex:
print(str(ex))
return {'time': None}
@app.route('/hola')
def time():
try:
return {'time': time.time()}
except Exception as ex:
print(str(ex))
return {'time': None}
Connected to pydev debugger (build 201.7846.77)
* Serving Flask app "app.py"
* Environment: development
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [01/Nov/2020 12:50:00] "GET / HTTP/1.1" 200 - 'function' object has no attribute 'time'
127.0.0.1 - - [01/Nov/2020 12:50:00] "GET /hola HTTP/1.1" 200 -