I'm trying to send a form using Ajax as JSON to a flask server, the problem is that the shape of the request I got on the server is strange. I'm following this answer and my code is as follows:
Form
<form action="/Go" role="form" method="post" id="myForm">
<div class="form-group">
<input autofocus class="form-control" id="fname" name="fname" placeholder="First Name" required type="text" value="">
</div>
<div class="form-group">
<input class="form-control" id="lname" name="lname" placeholder="Last Name" required type="text" value="">
</div>
<div class="form-group">
<input class="form-control" id="age" name="age" placeholder="Your Age" required type="text" value="">
</div>
<div class="form-group">
<input class="form-control" id="height" name="height" placeholder="What is your height" type="text" value="">
</div>
<div class="modal-footer shorten-form">
<button type="button" class="btn btn-secondary">Close</button>
<button type="submit" class="btn btn-primary">Create</button>
</div>
</form>
Jquery
$.ajax({
type: "POST",
url: "/Go",
data: JSON.stringify($("form#myForm").serializeArray()),
dataType:"json",
contentType:"application/json",
success: function (html, textStatus, jqXHR) {
...
}
});
Now using request.get_json()
on the server and printing the result appears to be like this:
[{'name': 'fname', 'value': 'Shadow'}, {'name': 'lname', 'value': 'fax'}, {'name': 'age', 'value': '12'}, {'name': 'height', 'value': ''}]
While I'm expecting something like this:
{'fname': 'Shodow', 'lname': 'fax', 'age': '12', 'height': ''}
What am I doing wrong?