![](/img/trans.png)
[英]Python and JSON Error - TypeError: string indices must be integers
[英]Error TypeError: string indices must be integers json jupyter kernel gateway
所以我的 ipynb 中有一個小的 api,代碼如下
# POST /convert
req = json.loads(REQUEST)
args = req['body']
print(args['angle'])
angle = args['angle']
converted = math.radians(angle)
print(json.dumps({'convertedAngle': converted}))
它在 postman 中運行良好,但是在前端網站中它給了我以下 2 個錯誤
CORS-header 'Access-Control-Allow-Origin' is missing
這個是合乎邏輯的,因為我的另一個錯誤使我不返回任何東西
Error TypeError: string indices must be integers
這個非常令人困惑,因為我什至不確定這個來自哪里
有沒有辦法解決這個問題,所以我可以從我的前端網站發布請求到我的 ipnb?
我知道的事情:
將其轉換為 GET function 允許它工作但非常不安全
postman 與以下 curl 請求:
curl --location --request POST 'http://127.0.0.1:8889/convert' \
--header 'Content-Type: application/json' \
--data-raw '{
"angle": 180
}'
k so i figured it out the problem was the pre flight options request from CORS it refused to send the correct cors headers back because you need to set that up in config or with the following long command in your anaconda prompt jupyter kernelgateway --KernelGatewayApp.api='kernel_gateway.notebook_http' --KernelGatewayApp.seed_uri='api.ipynb' --KernelGatewayApp.allow_headers="*" --KernelGatewayApp.allow_origin='*'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.