jQuery Ajax - PHP Script enthält mehrere echos, soll aber nur das letzte echo zurückgeben
bearbeitet von Regina SchaukrugWenn ich den all habe, dass ich mit dem selben Skript einmal die Webseite und einmal nur JSON-Daten ausliefern will, dann mach ich das in etwa so:
~~~JavaScript
loadScript('ajax-nur-ein-echo.php?api=1', function(data){
console.log( "loadScript", data);
});
~~~
~~~PHP
if ( ( ! isset( $_GET['api'] ) ) or $_GET['api'] != 1 ) {
header( 'Content-Type: text/html; charset=utf-8' );
echo "Das gehört nur zum HTML-Output";
echo "Das auch";
} else {
header( 'Content-Type: application/json; charset=utf-8' );
}
$hallo = "hallo";
$welt = "welt";
$phpResponse = array(
"hallo" => $hallo,
"welt" => $welt
);
echo json_encode( $phpResponse );
~~~
jQuery Ajax - PHP Script enthält mehrere echos, soll aber nur das letzte echo zurückgeben
bearbeitet von Regina Schaukrug~~~JavaScript
loadScript('ajax-nur-ein-echo.php?api=1', function(data){
console.log( "loadScript", data);
});
~~~
~~~PHP
if ( ( ! isset( $_GET['api'] ) ) or $_GET['api'] != 1 ) {
echo "Das gehört nur zum PHP Script";
echo "Das auch";
}
$hallo = "hallo";
$welt = "welt";
$phpResponse = array(
"hallo" => $hallo,
"welt" => $welt
);
header( 'Content-Type: application/json; charset=utf-8' );
echo json_encode($phpResponse);
~~~