1

So I am trying to send a value to my backend flask python and process the data there and return it back, but after trying for hours all I got are error codes, 400 or 500. Please help me!

Here's my js code:

var keyword = document.getElementById("keyword").value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', '/searchresults', true);
xmlhttp.send("keyword=" + keyword);
console.log("SUCCESSFULLY SENT");

and my python code:

@app.route("/searchresults")
def test():
 return request.args.get['keyword']

my form:

<form id="searchform">
<label for="keyword">Keyword <span class="required">*</span> </label>
<input type="text" id="keyword" name="keyword" size="15" required>
</form>

EDIT:

I checked the network to make sure that the request is sent. Please correct me if it's not. enter image description here

1 Answers1

1

You are trying to send a request body (xmlhttp.send("keyword=" + keyword)) with the keyword, but you are using the GET HTTP method, which does not allow a request body, so keyword=bla will never be part of the request.

Pass variables with a GET request

xmlhttp.open('GET', '/searchresults?keyword=' + keyword, true);
xmlhttp.send();

The Flask code is also wrong. .get() is a function, but it's being used as if it were a dictionary.

Call it as a function:

keyword = request.args.get('keyword')

Pass variables with a POST request

xmlhttp.open('POST', '/searchresults', true);
xmlhttp.send("keyword=" + keyword);

You'll have to change your Flask code to look in the request body instead of the args:

keyword = request.form.get('keyword')
Codebling
  • 10,764
  • 2
  • 38
  • 66
  • I tried using the GET request and when i tried to do "keyword = request.args.get('keyword')" in my backend, it still gives 500 internal server error when I visit link http://127.0.0.1:5000/searchresults. It is saying that request.args.get('keyword') returns a null. If I use request.args, then i see an empty dictionary. – excessive rice eater Mar 08 '20 at 18:56
  • @excessivericeeater hmm. Thinking. – Codebling Mar 08 '20 at 20:03
  • @excessivericeeater what is the error you're seeing from Flask? – Codebling Mar 08 '20 at 20:09
  • Works for me. What object type is `keyword`? Can you check the DevTools in your browser to ensure that `keyword` is being sent? Did you `from flask import request` ? – Codebling Mar 08 '20 at 20:13
  • The error is: TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement. and yes I did from flask import *. I checked the network in my DevTools and I am sure that it is being sent. What I'm trying to do is that I assigned the function to a button then when i click the button, the function sends the request. – excessive rice eater Mar 08 '20 at 20:21
  • There is no `view` function in the code you posted, so I'm not sure that I can provide any more help. This should get you 90% of the way there. Perhaps try printing `keyword` to the console in your Flask route before you return it. I suspect that it will be there, and that the problem lies elsewhere in your code. – Codebling Mar 08 '20 at 20:25
  • I uploaded a picture if you want to see it. Thank you so much for taking your time to help me, I am just really stuck on this part for HOURS. – excessive rice eater Mar 08 '20 at 20:25
  • Sure, I will look at the picture – Codebling Mar 08 '20 at 20:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209266/discussion-between-codebling-and-excessive-rice-eater). – Codebling Mar 08 '20 at 20:26