I want to return a string after my ajax code is executed. This is how my code looks like in my html
:
<script>
(function($){
$('#form').submit(function(event){
var formData = new FormData($("#form")[0]);
event.preventDefault();
$.ajax({
url: "/transform",
type: "POST",
data: formData,
success: function(data){
data
},
});
});
});(jQuery);
</script>
Basically what I want is to desplay the string that the method/transform
will return once it finished, something like "Done!", I want to see this message in localhost:5000
, not in the console.
To execute my code I access to localhost:5000
, however, when the message returned by /transform
is displayed the url is localhost:5000/transform
what makes me think ajax is not working.
For what I have read, by using Ajax my code is being executed asynchronously, therefore it can't automatically access to the return of /transform
. I have read about callbacks but still I don't get how can I use it, I am very new to this (I started yesterday and it is my first time using both html and js).
I also checked this question and answer How do I return the response from an asynchronous call? but still I can't see how can insert in my code the callback they are talking about.
Is there a way to do it?
This is the html I am using:
<html>
<body>
{% block content %}{% endblock %}
<div class="container">
<div class="col-md-6 center">
<div class="panel panel-default">
<div class="panel-body">
<form accept-charset="UTF-8" role="form" action="/transform" method="post" enctype="multipart/form-data" id="form" >
<div class="panel-heading">
<h3 class="panel-title" style="text-align:center">Select a file</h3>
</div>
<fieldset>
<div class="form-group">
<input class="form-control" type="file" name="data_file" />
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" name="submit" id="submit" value="Submit" placeholder="submit"/>
</fieldset>
</form>
</div>
</div>
</div>
</div>
</body>
</html>