使用ajax将python中的json返回到javascript时出现问题

使用ajax将python中的json返回到javascript时出现问题,javascript,python,jquery,ajax,flask,Javascript,Python,Jquery,Ajax,Flask,我正在将articleUrl从my.js发送到python函数,该函数运行良好。然后,我希望python函数将pubscore返回到.js pubscore在.py中打印得很好,但是我得到了“uncaughtreferenceerror:pubscore没有在Object.myFunction[作为成功](background.js:41)中定义”。第41行是.js中的var myPubscore=pubscore background.js $.ajax({ type: 'POST',

我正在将
articleUrl
从my.js发送到python函数,该函数运行良好。然后,我希望python函数将
pubscore
返回到.js

pubscore在.py中打印得很好,但是我得到了“uncaughtreferenceerror:pubscore没有在Object.myFunction[作为成功](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")}
        }
    }
请试试这个

功能工作(数据){
const myPubscore=data.greeting;
console.log(myPubscore)
如果(myPubscore>1){
log(“myPubscore大于1”)
}
}
chrome.runtime.onMessage.addListener(函数(请求、发送方、发送响应){
if(request.type==“articleUrl”){
var articleUrl=请求;
$.ajax({
键入:“POST”,
url:`${url}/buttoncolor`,
数据:articleUrl,
成功:嫁妆,
错误:函数(e){
console.log(“错误”,e)
}
})
}
})

谢谢,这让我走得更远了-我不再收到错误消息。但myPubscore打印为“未定义”。我尝试添加JSON.parse(myPubscore);但是我得到了“未捕获的SyntaxError:JSON中位置0处的意外标记u”,谢谢。在计算新行放在哪里时遇到了一些麻烦-它是放在success函数中,还是放在doWork函数中,还是完全分开?(它是否替换了其他函数之一?)我尝试将其作为一个单独的函数,但得到了“500(内部服务器错误)”嗯,仍然得到了“500(内部服务器错误)”。我正在粘贴更新的代码,包括上下文,以防我遗漏了什么。谢谢。首先,我确实需要AJAX将articleUrl从my.js发送到my.py。。。我在AJAX的后半部分遇到了问题,它将pubscore从.py发送到.jsI。我很困惑-我没有尝试从my.py获取数据:articleUrl,我正在尝试获取pubscore。我正在努力:1。将文章URL从js发布到py和2。从py到js获取pubscore。