0

I am creating a very simple dashboard for automating a task. Security is not a concern here because I am only local hosting.

JQuery Code

$.post("/approve", data={"title":title,"content":content}, success=function(){
      window.location.reload(true)
});

Flask Code

@app.post('/approve')
def approve_topic():
  args = request.args
  print(args)
  json.dump(args, open("topics/"+uuid4().hex+".json", "w"))
  return {"status":"clear"}

Result (the JSON file)

{}

Expected Result

{"title":"whatever the title is", "content":"whatever the content is"}

Any idea why this is happening? The first time I ran this it worked just fine, but now no matter what I reset or what I do it won't work.

OshiniRB
  • 578
  • 1
  • 7
  • 21
saundy
  • 43
  • 5
  • 1
    Side note: what is the point of using AJAX if you're just going to reload the page immediately afterwards? The whole purpose of AJAX is avoid needing to reload everything. If you want to do that then just so a normal postback to begin with, then you save the extra trip to the server. – ADyson Oct 09 '21 at 17:43
  • Try: `args = request.get_json(force=True)` – Faizan AlHassan Oct 09 '21 at 17:44
  • @ADyson The request is already a POST request, and I reload it to post request again. – saundy Oct 09 '21 at 17:45
  • `The request is already a POST request, and I reload it to post request again`... That wasn't my point. You're doing two requests when one would be sufficient. – ADyson Oct 09 '21 at 17:57
  • @FaizanAlHassan That gives a HTTP `400` – saundy Oct 09 '21 at 17:59
  • 1
    I suggest using a debugger to explore available options in the request object, like request.form, and check which of that is containing your data. if that doesn't work then make sure you're using the correct way to post JSON using jQuery https://stackoverflow.com/questions/5570747/jquery-posting-json – Faizan AlHassan Oct 09 '21 at 18:05
  • @FaizanAlHassan I've now done both of those and it is still not working. – saundy Oct 09 '21 at 18:29
  • So what results did you get from debugging then? That should have allowed you to see what was happening – ADyson Oct 09 '21 at 18:34
  • @ADyson The only things passed are `args`, which are `{}`. (besides other unrelated content) – saundy Oct 09 '21 at 19:11

1 Answers1

1

I just tested the code, it is working fine with request.form. Flask Code:

from flask import Flask, request
app = Flask(__name__)

# you can ignore this function as you're doing request on same origin
@app.after_request
def allow_cors_origin(response):
    """Resolve CORS error"""
    response.headers.add("Access-Control-Allow-Origin", "*")
    response.headers.add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,PATCH,OPTIONS")
    return response


@app.route("/echo", methods=["POST"])
def echo():
    data = request.form
    print(data)  # Output: ImmutableMultiDict([('a', '1')])
    return data 


app.run(debug=True)

jQuery Code:

$.post("http://localhost:5000/echo", {a: 1}, r=>console.log("response: ", r))
// Output: response:  {a: '1'}
Faizan AlHassan
  • 389
  • 4
  • 8