Passing json object to python using angularjs

Question!

I'm new to angularjs and python and I have this problem. I've been trying to pass in the data of a form to Python server side using angularjs. I've converted the form to a json object before sending it over in my .js controller.

controller.js:

    jsonObj = this.form.toJson;
    $xhr('POST','/form/processform',jsonObj,function() {
        alert("Done!");
        window.load("/");
    }, function(){
        "Request failed";
    });

Python:

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import simplejson as json

class processForm(webapp.RequestHandler):
    def post(self):
        form = json.loads(self.request.body)
        # process forms 
        self.redirect("#/")#redirects to main page

I recieved an error called "JSONDecodeError: No JSON object could be decoded". I've tried to replace the 'POST' to 'JSON' but it does not seem to work as well. I've also read up on $resource in angularjs, but i'm not sure how to use it.

Is this because of the wrong usage of $xhr? Any help will be greatly appreciated! :)



Answers

Acording to JSONDecodeError the jsonObj variable is not containing a valid JSON object.

I believe problem is here:

jsonObj = this.form.toJson;

You shoud call the toJson method instead of assiging it to a variable:

jsonObj = angular.toJson(this.form);


This video can help you solving your question :)
By: admin