否访问控制;标题存在于请求的资源上。使用JavaScript运行Python脚本时
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将资源添加到您的CORS(App,Resources = {r“ ”:{“ origins”:“ ”}}))
Try adding a resource to your CORS(app, resources={r"": {"origins": ""}})