jQuery ajax() success data - retrieving object results from Python server -


i trying display success data after jquery ajax function python server (gae). able make work single string or number success data, relay several pieces of info server in ajax call. thinking best way store data in python object , send object in callback. object showing "", object properties "undefined".
advice can give appreciated-

javascript:

        $.ajax({             type: "post",               url: "/update_cart",               data: {'item1': item1, 'item2': item2, 'item3': item3},              //datatype: 'jsonp',                             success: function(data) {                        $("#amount").val(data.total);         $("#shopping_cart_count").text(data.cart_item_count);                     alert(data);    //displays "<main.ajaxobject object @ 0x042c8890>"                     alert(data.cart_item_count);    //displays "undefined"                                   }                 }); 

python code:

    data = ajaxobject()      data.cart_item_count = 5       data.total = 10       logging.info("*****data.cart_item_count %d ******"  % data.cart_item_count )    #displays correctly      logging.info("*****data.total %d ******"  % data.total )  #displays correctly         self.response.out.write(data);  #respond ajax success data object 

you need json encode data shaunak d described. error you're getting telling json package doesn't know how encode ajaxobject instance string.

you can use custom json encoder fix detailed @ https://docs.python.org/2/library/json.html (section "extending jsonencoder")

alternatively (and more easily), can use dictionary represent data:

data = {     'cart_item_count': 5,     'total': 10 } self.response.out.write(json.dumps(data)) 

jquery's ajax function should automatically decode json string javascript object.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -