简体   繁体   English

通过Flask API的JSON POST无法接受我的参数

[英]POST of json over Flask API wont accept my argument

I have a problem with POSTing a JSON to my API... 我在向我的API发布JSON时遇到问题...

API: API:

app = Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": "*"}})

@app.route("/")
def hello():
    return jsonify({"about":"Hello wwworld"})

@app.route('/json-example', methods=['POST']) #GET requests will be blocked
def json_example():

    req_data = request.get_json()

    return jsonify(req_data)

AXIOS in Vue: Vue中的AXIOS:

  <script>
    var app = new Vue({
      el: '#app',
      data: {
        message: 'Hello Vue!',
        result:'',
        test:   {
                "language" : "Python",
                "framework" : "Flask",
                "website" : "Scotch",
                "version_info" : {
                    "python" : 3.4,
                    "flask" : 0.12
                },
                "examples" : ["query", "form", "json"],
                "boolean_test" : true
            }
      },
      methods:{

        getResults () {
            axios.get(`http://127.0.0.1:5000/`)
            .then(response => {this.result = response.data})
            .catch(error => {
              console.log(error)
            })
        },

        getResults2 () {
            axios.post(`http://127.0.0.1:5000/json-example`, {
              params: {
                test
              }
            })
            .then(response => {this.result = response.data})
            .catch(error => {
              console.log(error)
            })
        }

      }
    })
  </script>

For some reason i cant access test json that i would like to pass as an argument. 由于某种原因,我无法访问我想作为参数传递的测试 json。 First function getResults () works just fine but when I try getResults ()2 it gives me following error: 第一个函数getResults()正常工作,但是当我尝试getResults()2时,出现以下错误:

index.html:45 Uncaught ReferenceError: test is not defined index.html:45未捕获的ReferenceError:未定义测试

Can somone please give me a hint how to fix it? 可以给我个提示如何解决吗? I tried posting the json via Postman ans got a proper reponse. 我尝试通过Postman ans发布json,得到了适当的响应。

Thank you Jakub 谢谢雅库布

I think that you need to do this in getResults2 我认为您需要在getResults2执行此操作

    getResults2 () {
        axios.post(`http://127.0.0.1:5000/json-example`, this.test)
        .then(response => {this.result = response.data})
        .catch(error => {
          console.log(error)
        })
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM