Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

jquery - Ajax GET request not working with Flask

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

Here is the Flask code that serves the above request.

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

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.

This is the log that I am getting

  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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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

which will result in a request like:

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

This will be easily understood by the server.


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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...