Issue
I tried everything but the route just doesn't seem to work. I'm new to flask and I am running it locally. Also, my angular js code gives a 404 on posting the data to flask as the route doesn't seem to exist.
app.py
@app.route('/')
@app.route('/index')
def IndexPage():
return render_template('index.html')
#these routes don't work although all of the others do which is very confusing
@app.route('/misc')
@app.route('/contact/mama')
def printHello():
return render_template('index.html')
@app.route('/contact')
def ContactPage():
return render_template('contact.html')
@app.route('/about')
def AboutPage():
return render_template('about.html')
if __name__ == '__main__':
app.run()
Here is my angular code and the python script trying to handle the post request
var formApp = angular.module('formController', []);
formApp.controller("formControl", function($scope,$http) {
alert("mama");
$scope.FormSubmit = function ()
{
alert("In the function");
var data =
{
name : $scope.user.name,
phone : $scope.user.phone,
email : $scope.user.email,
message : $scope.user.message
};
var result = $http.post('contact/userData', data, null);
result.success(function(response)
{
const message = response.status;
alert(message)
alert("Thanks for contacting us");
});
result.error(function(data, status, headers, config)
{
console.log(result)
alert("Error while submitting data");
});
$scope.user.name = '';
$scope.user.phone = '';
$scope.user.email = '';
$scope.user.message = '';
};
});
and here is the python script to handle the request
import sys import app import json from flask import request
@app.route("/contact/userData", methods=['GET','POST'])
def SendMail():
message = json.dump({'status': 'success'})
return message
Solution
json.dumps() was causing a problem. Changed it to jsonify method and the code started working.
Answered By - Nikhil Mollay
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.