错误:连接被拒绝

huangapple go评论100阅读模式
英文:

POST Err_Connection_Refused

问题

我只会翻译你提供的内容,以下是翻译好的部分:

我只是想从我的JavaScript文件传输数据到Python。我尝试使用ajax请求来实现这一点,但是我得到了一个控制台错误:

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

这是我负责从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)
      }); 
    } 

这是我的Python代码:

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)

Flask应用程序默认在http://127.0.0.1:5000/上启动:
错误:连接被拒绝

所以我无法想象为什么会出现这个问题。

英文:

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/ :
错误:连接被拒绝

so i cant imagine why i have this problem

答案1

得分: 0

如果你的JavaScript代码在与Flask服务器不同的域或端口上执行,你可能会遇到CORS问题

你需要正确设置CORS,可以使用flask-cors包来处理CORS,像这样:

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

app = Flask(__name__)
CORS(app)

可以参考以下链接:

英文:

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:

huangapple
  • 本文由 发表于 2023年8月9日 02:58:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定