I'm sending articleUrl from my .js to my python function, which works fine. I then want my python function to return pubscore back to the .js.
pubscore prints fine in the .py, but then I get "Uncaught ReferenceError: pubscore is not defined at Object.myFunction [as success] (background.js:41)". Line 41 is var myPubscore = pubscore in the .js.
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
Suggested code that didn't work for me but could be helpful to someone else
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")}
}
}