否访问控制;标题存在于请求的资源上。使用JavaScript运行Python脚本时

发布于 2025-02-10 22:47:34 字数 1126 浏览 2 评论 0原文

我是Ajax和Flask的新手,我正在尝试制作一个Chrome Extension,该扩展是从JavaScript中运行Python脚本的。脚本以某个坐标移动鼠标,然后单击此处。

这是JavaScript代码:

function click_coord() {
    $.ajax({
        url: "http://127.0.0.1:5000/click_coord/",
        type: "POST",
        success: function(resp){
        console.log(resp);
    }
    });
 }

这是Python代码:

from flask import Flask, jsonify
from flask_cors import CORS, cross_origin
from pynput.mouse import Button, Controller

app = Flask(__name__)
cors = CORS(app) 
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route('/click_coord/', methods=['POST']) 
def click_coord():
    move_mouse()
    a = 15
    b = 17
    return jsonify(a+b)

def move_mouse():
    mouse = Controller()

    mouse.position = (201, 549)

    mouse.press(Button.left)
    mouse.release(Button.left)

if __name__ == "__main__":
    app.run(debug=True)

我遇到的错误是:在请求的资源上没有“访问控制”标头。我只有在将函数称为“ move_mouse”时才会出现此错误。如果不这样做,代码将正确运行。

我在另一篇文章中看到一个人有问题,因为他们没有添加正确的权限,但我添加了:“权限”:[“ TABS”,“ http:// localhost/*”]

I am new to AJAX and Flask and I am trying to make a Chrome extension that runs a Python script from Javascript. The script moves the mouse at a certain coordinate and clicks there.

This is the Javascript code:

function click_coord() {
    $.ajax({
        url: "http://127.0.0.1:5000/click_coord/",
        type: "POST",
        success: function(resp){
        console.log(resp);
    }
    });
 }

This is the Python code:

from flask import Flask, jsonify
from flask_cors import CORS, cross_origin
from pynput.mouse import Button, Controller

app = Flask(__name__)
cors = CORS(app) 
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route('/click_coord/', methods=['POST']) 
def click_coord():
    move_mouse()
    a = 15
    b = 17
    return jsonify(a+b)

def move_mouse():
    mouse = Controller()

    mouse.position = (201, 549)

    mouse.press(Button.left)
    mouse.release(Button.left)

if __name__ == "__main__":
    app.run(debug=True)

The error I get is: No 'Access-Control-Allow-Origin' header is present on the requested resource. I only get this error if I call the function "move_mouse". If I don't, the code runs properly.

I saw in another post that a person had a problem because they didn't add the correct permissions but i added: "permissions": ["tabs", "http://localhost/*"] to the Json manifest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪 2025-02-17 22:47:34

尝试将资源添加到您的CORS(App,Resources = {r“ ”:{“ origins”:“ ”}}))

Try adding a resource to your CORS(app, resources={r"": {"origins": ""}})

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文