Ajax GET请求不适用于Flask [英] Ajax GET request not working with Flask

查看:126
本文介绍了Ajax GET请求不适用于Flask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用html接收来自用户的一堆输入,然后将其传递给ajax查询以获取响应.

I am taking in bunch of inputs from the user in html which I am then passing on to ajax query to get the response.

$.ajax({
  url:"http://0.0.0.0:8080/getReport",
  type:"GET",
  data:JSON.stringify(out),
  dataType:"json",
  contentType:"application/json"
})

以下是满足上述要求的Flask代码.

Here is the Flask code that serves the above request.

@app.route('/getReport', methods=['GET'])
def report():
    return Response('this is a sample response')

上述方法无法通过get找到到报告"的路线.但是,它可以在POST请求中找到它.

The above method is not able to find the route to the 'report' with get. However, it is able to find it in POST request.

这是我得到的日志

  127.0.0.1 - - [25/Apr/2016 13:00:03] "GET /getReport?{%22insertion_id%22:%22%22,%22start%22:%22%22,%22end%22:%22%22} HTTP/1.1" 400 -

错误请求..我在这里做什么错了?

Bad Request.. What am I doing wrong here?

推荐答案

GET请求没有contentType (*),并且它不是JSON编码的,而是URL编码的(普通的常规键值对).

A GET request does not have a contentType (*) and it isn't JSON-encoded, but URL-encoded (plain, regular key-value pairs).

这意味着您可以简单地使用jQuery的默认值.

This means you can simply go with jQuery's default.

$.get("http://0.0.0.0:8080/getReport", out).done(function (data) {
    // request finished
});

这将导致如下请求:

GET /getReport?insertion_id=&start=&end= HTTP/1.1

服务器会很容易理解.

(*)这是因为Content-Type标头确定了请求正文的类型. GET请求没有请求正文.

(*) That's because the Content-Type header determines the type of the request body. GET requests do not have a request body.

这篇关于Ajax GET请求不适用于Flask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆