title: Use Jquery $.ajax to call a server side function on Flask
body: <p>I want to call a server side function using Ajax. </p>
  
  <p>I have found a straightforward example for PHP <a href="https://stackoverflow.com/questions/2269307/using-jquery-ajax-to-call-a-php-function">in this post</a>. I think the community would improve if we could include this very same example but for Python / Flask MVC framework.  </p>
  
  <p>This is the ajax code on the View side, called test.html:</p>
  
  <pre><code>&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"&gt;&lt;/script&gt;
  
  &lt;script&gt;
  function create () {
  $.ajax({
  url:"test1", //the page containing python script
  type: "post", //request type,
  dataType: 'json',
  data: {registration: "success", name: "xyz", email: "abc@gmail.com"},
  success:function(result){
  console.log(result.abc);
        }
      });
     } 
  &lt;/script&gt;
  </code></pre>
  
  <p>And this would be the Python code on the Controller:</p>
  
  <pre><code>@app.route('/test', methods=['GET','POST'])
      def test():
          return render_template("test.html", brand = brand)
  
  @app.route('/test1', methods=['GET','POST'])
      def test1():
          if registration == "success":
              return json.dump({"abc":'successfuly registered'});
  </code></pre>