Ich bekomme eine Zeichenfolge von file_get_contents('php://input')
. Ich versuchte json_decode()
, aber die Zeichenfolge ist kein JSON. Hier ist die Ajax-Anfrage und der PHP-Code. Wie kann ich den JSON von der AJAX-Anfrage erhalten und ihn in ein PHP-Array umwandeln?"php: // input" returning string für json ajax anfrage
$data = file_get_contents('php://input');
var_dump($data);
echo $data;
Ausgang:
string(7) "id=myId"
"id=myId"
Ajax (Enthält JQuery):
$.ajax({
"url": "myFile.php",
"type": "POST",
"contentType": "Json",
"data": {"id": "myId"},
}).done(function(data, status) {
if (status == "success") {
console.log(data);
}
}).fail(function(data, status, error) {
throw new Error(error);
console.log(data);
console.log(status);
});
Edit: json_encode()
kehrt null, so kann ich nicht die Antwort auf dieser Frage verwenden: PHP: file_get_contents('php://input') returning string for JSON message
Mögliches Duplikat von [PHP: file \ _get \ _contents ('php: // input'), das die Zeichenfolge für die JSON-Nachricht zurückgibt] (https://stackoverflow.com/questions/21974951/php-file-get-contentsphp- input-returning-string-for-json-message) – Woodrow
Es ist form-codiert. Sie können * es * selbst entschlüsseln oder einfach über '$ _POST ['id']' darauf zugreifen, wo PHP es bereits entschlüsselt hat. – Sammitch
'$ _POST [" data "]' und '$ _POST [" id "]' funktionierte nicht für mich, also musste ich 'file_get_contents ('php: // input')' verwenden, was den Job – Rishi