【问题标题】:trouble returning json from python to javascript with ajax使用ajax将json从python返回到javascript时出现问题
【发布时间】: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


    【解决方案1】:

    请试试这个

    function doWork(data) {
      const myPubscore = data.greeting;
      console.log(myPubscore)
      if (myPubscore > 1) {
        console.log("myPubscore is more than 1")
      }
    }
    
    chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
      if (request.type == "articleUrl") {
        var articleUrl = request;
        $.ajax({
          type: 'POST',
          url: `${url}/buttoncolor`,
          data: articleUrl,
          success: doWork,
          error: function(e) {
            console.log("error", e)
          }
        })
      }
    })
    

    【讨论】:

    • 谢谢,这让我走得更远——我不再收到错误消息了。但 myPubscore 打印为“未定义”。我尝试添加 JSON.parse(myPubscore);但后来我得到“未捕获的语法错误:位置 0 的 JSON 中的意外标记 u”
    • 谢谢。在确定将新行放在哪里时有点麻烦——它是放在成功函数中,还是放在 doWork 函数中,还是完全分开? (它会取代其他功能之一吗?)我将它作为一个单独的功能尝试,但得到“500(内部服务器错误)”
    • 嗯,仍然收到“500(内部服务器错误)”。我正在粘贴更新的代码,包括上下文,以防我遗漏了什么。
    • 谢谢。我确实需要 AJAX 首先将 articleUrl 从我的 .js 发送到我的 .py ...我在 AJAX 的后半部分遇到问题,它将 pubscore 从 .py 发送到 .js
    • 我很困惑——我不想从我的 .py 中获取数据:articleUrl,我想获取 pubscore。我正在尝试: 1. 将 articleUrl 从 js 发布到 py 和 2. 从 py 获取 pubscore 到 js。
    猜你喜欢
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2013-01-23
    相关资源
    最近更新 更多