【问题标题】:Chrome extension client side showing successful but server generating 500 errorChrome 扩展客户端显示成功但服务器生成 500 错误
【发布时间】:2020-12-28 03:35:35
【问题描述】:

我已经被困了半天了,因为我不确定为什么在一个控制台上它是成功的,而在另一个控制台上却没有。

我正在尝试从 chrome 扩展/客户端向烧瓶服务器发起 POST 调用。

background.js

fetch('http://0.0.0.0:5000/test',
    {
        method:'POST',
            body: JSON.stringify({"name":"daniel"}),
        headers: {
          "Content-Type": "application/json;charset=UTF-8",
          'Accept': 'application/json'
        }       
    })
    .then(response => response.json())
  .then(json => console.log(JSON.stringify(json))); 

manifest.json

{
    "manifest_version": 2,
    "name": "To be named",
    "description": "This extension helps...",
    "version": "0.1.0",
    "permissions": [
        "identity",
        "identity.email",
        "http://0.0.0.0:5000/test"
    ],
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },  
    "content_scripts": [{
        "matches": ["https://www.youtube.com/"], 
        "js": ["content.js"],
        "css": ["styles.css"]
    }]
}

PY(烧瓶)

@blueprint.route('/test', methods=['GET', 'POST'])
def test():
    if request.method == 'POST':
        return request.json#{"status": "OK", "status_code": 200, "data": data}
    elif request.method == 'GET':
        return request.json
    else:
        return 'nothing printed'

目前尝试的解决方案:

  • request.loads/dumpts(request.json)

    在烧瓶上安装 CORS

    将 Content-Type 更改为 Content-'t'type

^所有这些解决方案只是返回 null 作为响应而不是错误,并且没有得到后台控制台中显示的实际数据

后台js控制台: enter image description here

测试开发:

enter image description here

TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

在控制台上:

Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR)

null screen shot

【问题讨论】:

    标签: javascript http flask google-chrome-extension fetch


    【解决方案1】:

    为您的test 路由尝试以下代码:

    @blueprint.route('/test', methods=['GET', 'POST'])
    def test():
        if request.method == 'POST':
            response_dict = {
                "status": "OK",
                "status_code": 200,
                "data": data 
            }
            return jsonify(response_dict) # Import jsonify: from flask import jsonify
        elif request.method == 'GET':
            return jsonify(request.json)
        else:
            return 'nothing printed'
    
    

    request.json 只是使用默认参数调用request.get_json(),它会尝试将请求数据解析为 JSON,它不会返回 Response,这就是为什么它是无效响应。

    如果您想向客户端返回一些 JSON,jsonify 是一个方便的函数。

    文档:https://flask.palletsprojects.com/en/1.1.x/api/#flask.json.jsonify

    【讨论】:

    • 感谢您的回复。不幸的是,我仍然从该代码中得到null
    • null 似乎是什么?请分享屏幕截图或其他内容。
    • 感谢您回来,例如,我已在帖子末尾粘贴了屏幕截图的链接。此外,我已经在 chrome 扩展上尝试了你的示例,但也没有成功:(
    • 结果:AttributeError: 'NoneType' object has no attribute 'get'
    猜你喜欢
    • 2012-05-19
    • 1970-01-01
    • 2019-01-20
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    相关资源
    最近更新 更多