php - Post request on Slim Framework -
could please me? have 500 error when making post request... can't understand is
note: if run api on rest client chrome extension works otherwise following error...
the application not run because of following error: details type: errorexception code: 8 message: trying property of non-object line:114
routes:
<?php require 'slim/slim.php'; \slim\slim::registerautoloader(); $app = new \slim\slim(); $app->get('/events','getevents'); $app->get('/events/:year/:month', 'getmonth'); $app->get('/events/:year/:month/:day','getallafter'); $app->post('/events', 'addevent'); $app->run();
this function:
function addevent() { $app = \slim\slim::getinstance(); $request = $app->request(); $body = $request->getbody(); $event = json_decode($body); //line 114 $submited_date = $submited_date = $event->{'date_submit'} .' '.$event->{'time_submit'}; $sql = "insert events (edate, title, performers, address) values (:edate, :title, :performers, :address)"; try { $conx = getconx(); $stmt = $conx->prepare($sql); $stmt->bindparam("edate", $submited_date); $stmt->bindparam("title", $event->etitle); $stmt->bindparam("performers", $event->performers); $stmt->bindparam("address", $event->address); $stmt->execute(); $event->id = $conx->lastinsertid(); $conx = null; $result = array("status"=>"success","events"=>$event); echo json_encode($result); } catch(pdoexception $e) { $result = array("status"=>"error","message"=>'exception: ' . $e->getmessage()); echo json_encode($result,json_pretty_print); } }
this json sent:
{ "date":"24 march, 2014", "date_submit":"2014-03-24", "time":"4:00 pm", "time_submit":"16:00:00", "etitle":"event title", "performers":"david", "address":"place" }
jquery code: fixed using json.stringify(); data before sending request
function addevent(jsondat) { console.log('addevent'); $.ajax({ type: 'post', contenttype: 'application/json', url: rooturl, datatype: "json", data: json.stringify(jsondat); , success: function(data, textstatus, jqxhr){ alert(event created successfully'); }, error: function(jqxhr, textstatus, errorthrown){ alert('addevent error: ' + textstatus); } }); } jquery(document).on('ready', function() { jquery('form#myform').bind('submit', function(event){ event.preventdefault(); var form = this; var pson = convertformtojson(form); //document.write(json.stringify(pson)); addevent(pson); }); });
the problem found , wasn't in index.php in ajax request... fixed using json.stringify() serialized array.
that's why in rest client worked because json there sent correctly... matt slim framework support http://help.slimframework.com/discussions/problems/6789-not-able-to-handle-post-request
Comments
Post a Comment