Прошел кругами, пытаясь получить вызов AJAX на сервер PHP. Ближе, чем я был несколько часов назад. Все еще тупик относительно того, как получить доступ к данным $ _POST в PHP с любым смыслом.PHP REST server - AJAX - JSON
Мой Аякса из
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???
}
Это попадает в PHP, как и в я могу сделать это
$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;
Это делает его обратно к моей стороне клиента кода, а след от PHP-код выглядит как это.
{"packed":" {\"type\":\"USER\",\"method\":\"LOGIN\",\"email\":\"root\",\"pw\":\"S0lari s7.1\",\"remember\":\"false\"}"}
Таким образом, похоже, что материал проходит через большой разрыв. КАК, в моем PHP-коде я могу проверить значение «type», чтобы узнать, является ли он «USER»? Я все кодировал и декодировал, но, похоже, это не получается!
Cheers, Mark.
'$ _POST ['упакованный'] ['type']' ?? – jszobody
Сделайте себе одолжение, просто поместите 'print_r ($ _ POST)' или 'var_dump ($ _ POST)' в верхней части страницы PHP, которая получает запрос AJAX. Выполните запрос и внимательно посмотрите на данные, напечатанные на экране. Ознакомьтесь с тем, как данные формы отправляются на ваш PHP, включая то, что передается, а что нет. Если вы отправляете JSON, вам нужно будет использовать 'json_decode()', чтобы правильно получить значения. –