JQuery ajax () POST for Slim PHP framework

Using jquery mobile + phonegap, trying to do a POST for a Slim application, I have this code:

$( document ).on( "vclick", "#test_form", function() { $.ajax({ type: "POST", url: "http://mydomain.com/slim/", crossDomain: true, beforeSend: function() { $.mobile.loading('show') }, complete: function() { $.mobile.loading('hide') }, data: {namec:$("#namec").val()}, dataType: 'json', success: function(response) { //console.error(JSON.stringify(response)); alert(response); }, error: function() { //console.error("error"); alert('Not working!'); } }); }); 

I tested this with other pages than Slim PHP and everything works fine, I get an ajax error using Slim.

My Slim app:

 <?php require 'Slim/Slim.php'; \Slim\Slim::registerAutoloader(); $app = new \Slim\Slim(); $app->post('/', function () { echo json_encode($_POST("namec")); }); $app->run(); 

Just started using Slim, so I'm not sure what I can do wrong.

+4
source share
2 answers

You tried:

 $app->post('/', function() use ($app) { // ... $req = $app->request(); echo json_encode($req->post('namec')); //... } 

Also this page should help

+8
source

According to slim v3, you can access the request object using $ request-> getParams ()

 $app->get('/', function (Request $request, Response $response){ $data = $request->getParams() echo json_encode($data['namec']); } 

see the documentation here https://www.slimframework.com/docs/v3/objects/request.html#route-object

0
source

Source: https://habr.com/ru/post/1485340/


All Articles