Send php response to ajax
29,398
Solution 1
<?php
if ($success){
$result = array("status" => "1");
echo json_encode($result);
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=/404.html\">";
}
?>
<script>
jQuery(document).ready(function() {
$.ajax({
type: 'GET',
url: 'Thatscriptsomething.php',
cache: 'false',
dataType: 'json',
success: function(response) {
if(response.status == "1") {
alert("we having a working script");
} else {
alert("Oops, script is a no go");
}
}
});
});
</script>
Solution 2
Basic example - it works for me
PHP RESPONSE
$value = array('msg' => 'true' );
echo json_encode($value);
AJAX METHOD
$.ajax({
type: 'post',
url: 'URL',
contentType: false,
processData: false,
dataType:'JSON',
data: formData,
success: function(value) {
if (value.msg == 'true') {
//your action
}else{
//your action
}
}
});
Author by
lostAstronaut
Updated on July 18, 2022Comments
-
lostAstronaut almost 2 years
Okay I have a php script which ends as so :
if ($success) { $result = array('success' => true); } else { $result = array('success' => false, 'message' => 'Something happened'); header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); } echo json_encode($result);
And some jquery that I was planning on having alert me when my script is working.
jQuery(document).ready(function() { $.ajax({ url: './contactengine.php', type: 'GET', dataType: 'JSON', success: function(response) { alert("GOOD"); }, error: function() { alert("BAD"); } }); });
edited source