Been going around in circles trying to get an AJAX call to a PHP server happening. Closer than I was a few hours ago. Still stumped as to how to access the $_POST data in PHP with any sense.
My ajax out
var __private_api = function() {
// workhorse. EVERY call to our server REST API comes through
// this interface. NO exceptions. We will NOT have an interface per
// form or per MODEL as is a common case. Makes no sense having an API really.
// Might as well just code willy7-nilly.
var packed = JSON.stringify(packet);
console.log(packed);
jqxhr = $.ajax({
method: "POST", // ALL API calls are POST
url: "oop/server.php/", // API code
dataType: "json", // JSON back at us please
//contentType: "application/json", // HEADER. IMPORTANT!
data: {"packed":packed} // form data + security packet
}).fail(function(msg) { // error is depreciated. WHY? Dunno...
alert("Database Communication failure"); // this is a HARD failure sent to
console.log(JSON.stringify(msg)); // us by the comms stack, authentication, or OS
}).done(function(data) { // AJaX worked, deal with the resultant packet
__private_callback(data); // in our custom callback. success() has been
}); // depreciated, replaced by bone(). Why???
}
This gets to PHP as in I can do this
$this->database->log_config->trace(json_encode($_POST));
$this->crud = 'EXEC';
$SQL = "SELECT * from patients limit 2";
switch($this->crud) {
case 'EXEC': // before we drop into CRUDDiness, handle
$this->database->execute($SQL); // well? This eithe dies or comes back
// it CAN come back with an empty SET
// that is an SEP.
$db_result = $this->database->fetch_all(); // retrieve tuples from the CURSOR
//$this->response['data'] = $db_result->get_stack(); // and shove in the parcel to go back to the CLIENT API
$this->response['data'] = json_decode(json_encode($_POST),true); // and shove in the parcel to go back to the CLIENT API
break;
That makes it back to my client side code, and the trace from the PHP code looks like this.
{"packed":" {\"type\":\"USER\",\"method\":\"LOGIN\",\"email\":\"root\",\"pw\":\"S0lari s7.1\",\"remember\":\"false\"}"}
So stuff seems to be making it over the great divide OK. HOW, in my PHP code can I test the value of 'type' to see if it is 'USER'? I have been all over encode and decode but just don't seem to be getting it!
Cheers, Mark.
$_POST['packed']['type']??print_r($_POST)orvar_dump($_POST)at the top of the PHP page which receives the AJAX request. Perform the request and look closely at the data printed to the screen. Familiarize yourself with how form data is posted to your PHP, including what gets passed and what doesn't. If you're sending JSON you'll have to usejson_decode()to get to the values properly.