0

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")}
        }
    }
0

1 Answer 1

1

Please try this

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)
      }
    })
  }
})
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, that got me farther — I'm not getting the error message anymore. But myPubscore prints as "undefined". I tried adding JSON.parse(myPubscore); but then I get "Uncaught SyntaxError: Unexpected token u in JSON at position 0"
Thanks. Having a bit of trouble figuring out where to put that new line — does that go inside the success function, go in the doWork function, or is it separate altogether? (Does it replace one of the other functions?) I tried it as a separate function but got "500 (INTERNAL SERVER ERROR)"
Hmm, still getting the "500 (INTERNAL SERVER ERROR)". I'm pasting the updated code including the context in case I'm missing something.
Thanks. I do need the AJAX to send the articleUrl from my .js to my .py in the first place ... I'm having trouble with the second half of the AJAX, which is sending pubscore from the .py to the .js
I'm confused — I'm not trying to get data: articleUrl from my .py, I'm trying to get pubscore. I'm trying to: 1. Post articleUrl from js to py and 2. Get pubscore from py to js.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.