Passing json object to python using angularjs

I am new to angularjs and python and I have this problem. I am trying to submit form data to a Python server using angularjs. I converted the form to json object before submitting it 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 got the error "JSONDecodeError: JSON object could not be decoded". I tried to replace "POST" with "JSON" but it doesn't seem to work. I've also read up on $ resource in angularjs, but I'm not sure how to use it.

Is this due to misuse of $ xhr? Any help would be appreciated! :)

+3


source to share


1 answer


As per the JSONDecodeError

variable jsonObj

does not contain a valid JSON object.

I believe the problem is here:

jsonObj = this.form.toJson;

      



You call the method toJson

instead of specifying a variable:

jsonObj = angular.toJson(this.form);

      

0


source







All Articles