0

I just want to transfer data from my javascript file to python. I tried to do this via ajax request, but I get a console error:

POST http://127.0.0.1:5000/ net::ERR_CONNECTION_REFUSED

here is a part of my code responsible for transferring data from js:

function getAccountInfo () {
      
      const number = 5
      const message = 'some text'

      const dict_values = {number, message}
      const s = JSON.stringify(dict_values);
      console.log(s)
      $.ajax({
        url:"http://127.0.0.1:5000/test",
        type:"POST",
        contentType:"application/json",
        data: JSON.stringify(s)
      }); 
    } 

that is my python code:

from flask import Flask, render_template, request, redirect, session
import json

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')

@app.route('/test', methods=['POST'])
def test():
    output = request.get_json()
    print(output)
    print(type(output))
    result = json.loads(output)
    print(result)
    print(type(result))

    return result

if __name__ == '__main__':
    app.run(debug=True)

the flask application by default is launched on http://127.0.0.1:5000/ : enter image description here

so i cant imagine why i have this problem

Diego Borba
  • 1,282
  • 8
  • 22
why
  • 31
  • 5

1 Answers1

0

If your JavaScript code is executing on a different domain or port than your Flask server, you might encounter CORS issues.

You need to set up CORS properly, you can use the flask-cors package to handle CORS, this way:

from flask import Flask, render_template, request, redirect, session
from flask_cors import CORS
import json

app = Flask(__name__)
CORS(app)

Check this out:

Diego Borba
  • 1,282
  • 8
  • 22
  • this helped and I don't get any more errors, but for some reason python still doesn't get my variables. – why Aug 08 '23 at 21:36
  • @why You mean that it doesn't recognize the payload? – Diego Borba Aug 08 '23 at 22:58
  • yes, it does not output any messages that it should output when receiving a request – why Aug 09 '23 at 07:50
  • Did you debug to see if the problem is with the values or the `print()`? – Diego Borba Aug 09 '23 at 12:10
  • Yes, I tried, but I didn't get any notification from the debugger – why Aug 09 '23 at 22:03
  • lets talk about it in [chat](https://chat.stackoverflow.com/rooms/254870/post-err-connection-refused) – Diego Borba Aug 10 '23 at 13:36
  • due to the fact that I'm new to stackoverflow, I don't have enough reputation to talk in messages. I have debugged again and now every time I call getAccountInfo() I receive this message: `127.0.0.1 - - [10/Aug/2023 21:17:05] " [ 33mOPTIONS /test HTTP/1.1 [0m" 404 -` on my python shell – why Aug 10 '23 at 18:57
  • In my tests it works... is there any way we can communicate directly? So as not to fill the answer with out-of-scope comments. – Diego Borba Aug 10 '23 at 19:05
  • I can show you my code on dicrord stream so you can find the error easier i guess(discord: whycringe) – why Aug 10 '23 at 20:48
  • solicitation sended! – Diego Borba Aug 10 '23 at 20:52