簡體   English   中英

錯誤類型錯誤:字符串索引必須是整數 json jupyter kernel 網關

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM