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
Post a Comment