【发布时间】:2020-05-13 13:50:18
【问题描述】:
我正在将 articleUrl 从我的 .js 发送到我的 python 函数,它工作正常。然后我希望我的 python 函数将pubscore 返回到 .js。
pubscore 在 .py 中打印良好,但随后我得到 “未捕获的 ReferenceError:pubscore 未在 Object.myFunction [as success] (background.js:41) 中定义”。第 41 行是 .js 中的 var myPubscore = pubscore。
background.js
$.ajax({
type: 'POST',
url: `${url}/buttoncolor`,
data: articleUrl,
success: function urlFunction(data) {
var myPubscore = pubscore;
console.log(myPubscore);
}
})
application.py
def buttoncolor():
import json
if request.method == 'POST':
if not request.form['url']:
flash('Please enter all the fields', 'error')
else:
rurl = request.form['url']
...
pubscore = pub_tuple[8]
print(pubscore)
return json.dumps(pubscore)
else:
strscore = str(pubscore)
message = {'greeting': strscore}
return jsonify(message) # serialize and use JSON headers
对我不起作用但可能对其他人有帮助的建议代码
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.type == "articleUrl") {
var articleUrl = request;
$.ajax({
type: 'POST',
url: `${url}/buttoncolor`,
data: articleUrl,
success: function(){ alert('success');
}
})
$.getJSON(`${url}/buttoncolor`,{data: articleUrl}, function(data) {
doWork(data.greetings);
});
function doWork(myPubscore){
console.log(myPubscore);
if (myPubscore > 1)
{console.log("myPubscore is more than 1")}
}
}
【问题讨论】:
标签: javascript python jquery ajax flask