javascript - sending JSON to google app engine datastore -
so i've been scouring interwebs , cant find works me. have simple html form need send via json app engine datastore.
html form:
<form method="get" name="vehicle-form" id="vehicle-form"> <input type="text" name="type" placeholder="vehicle type" requried> <input type="text" name="make" placeholder="make" requried> <input type="text" name="model" placeholder="model" requried> <input type="text" name="year" placeholder="year" requried> <button id="submit-btn">add</button> </form>
javascript: edit: changed this- values going in null
var formdata = {}; $("#part-form").serializearray().map(function (x) { formdata[x.name] = x.value; }); //var par = document.getelementbyid("test2"); //par.innerhtml += "<p>"; //for (var in formdata) { // par.innerhtml += formdata[i] + "<br>"; //} //par.innerhtml += "</p>"; var uristring = myurl; $.ajax({ url: uristring, type: "post", //data: json.stringify(formdata) data: { "type" : formdata[0], "quantity" : formdata[1], "cost" : formdata[2], "vehicle_id" : formdata[3] } });
i doing on windows 8 phone, , success function doesn't execute, , no data added datastore.
here backend code post request
class addvehicle(webapp2.requesthandler): def post(self): """creates vehicle entity post vehicle variables: vehicle_type = vehicle type - required make = make - requried model = model - required year = year - required """ new_vehicle = vehicle() vehicle_type = self.request.get('type', default_value=none) make = self.request.get('make', default_value=none) model = self.request.get('model', default_value=none) year = self.request.get('year', default_value=none) # required info new_vehicle.vehicle_type = vehicle_type new_vehicle.make = make new_vehicle.model = model new_vehicle.year = year self.response.write(new_vehicle) # post data datastore key = new_vehicle.put() out = new_vehicle.to_dict() self.response.write(json.dumps(out)) return
this ndb model
class vehicle(ndb.model): vehicle_type = ndb.stringproperty(required=true) make = ndb.stringproperty(required=true) model = ndb.stringproperty(required=true) year = ndb.stringproperty(required=true)
got work:
$.ajax({ url: uristring, type: "post", contenttype: "application/x-www-form-urlencoded", //<-- data: { type: vehicle_type, make: vehicle_make, model: vehicle_model, year: vehicle_year }, success: function (result) { winjs.navigation.navigate("/pages/vehicleadd/vehicleadd.html"); } });
turns out needed send url encoded form data instead of json...
Comments
Post a Comment