Ich habe folgende Ajax-Anfrage:Umschalten von GET POST
// JavaScript
function myFunc(pid) {
$.ajax({
type : "GET",
url : "testback.php",
contentType : "application/json; charset=utf-8",
dataType : "json",
data : {
q : "testrequest",
pid : pid
},
success : function(data) {
console.log(data)
},
error : function(jqXHR, status, error) {
console.log(status, error);
}
});
}
// PHP
require_once ("dbconnect.php");
if (isset ($_GET ['q'])) {
if ($_GET ['q'] == "testrequest") {
$pid = $_GET ['pid'];
$query = "SELECT * FROM `tab1` WHERE `pid` = " . $pid;
$json = array();
if ($result = $link->query ($query)) {
while ($row = $result->fetch_assoc()) {
array_push ($json, $row);
}
}
header ("Content-type: application/json; charset=utf-8");
die (json_encode ($json));
exit();
}
die();
}
Es hat eine Anfrage an meine MySQL-Datenbank sendet und gibt die erwartete Ausgabe.
Allerdings möchte ich jetzt auf POST
statt GET
wechseln.
Wenn ich tauschen nur mit POST GET:
// JavaScript
function myFunc(pid) {
$.ajax({
type : "POST", // POST
url : "testback.php",
contentType : "application/json; charset=utf-8",
dataType : "json",
data : {
q : "testrequest",
pid : pid
},
success : function(data) {
console.log(data)
},
error : function(jqXHR, status, error) {
console.log(status, error);
}
});
}
// PHP
require_once ("dbconnect.php");
if (isset ($_POST ['q'])) { // POST
if ($_POST ['q'] == "testrequest") { // POST
$pid = $_POST ['pid']; // POST
$query = "SELECT * FROM `tab1` WHERE `pid` = " . $pid;
$json = array();
if ($result = $link->query ($query)) {
while ($row = $result->fetch_assoc()) {
array_push ($json, $row);
}
}
header ("Content-type: application/json; charset=utf-8");
die (json_encode ($json));
exit();
}
die();
}
ich folgende Fehlermeldung in der Konsole:
parsererror SyntaxError: Unexpected end of JSON input
Die Anfrage Nutzlast ist noch q=testrequest&pid=1
.
Was muss ich noch ändern, um von GET
zu POST
zu wechseln?
Verwendung 'json_decode()' in Ihrer _POST Abfrage $ – ixe
@ ixe: Kannst du mir sagen, wo genau? – user1170330
@ user1170330 Einstellungen 'contentType' und' dataType' entfernen – postrel