Send php response to ajax

Ok, I have a php script that ends like this:

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 planned to warn when my script is running.

  jQuery(document).ready(function() { $.ajax({ url: './contactengine.php', type: 'GET', dataType: 'JSON', success: function(response) { alert("GOOD"); }, error: function() { alert("BAD"); } }); }); 

edited source

+6
source share
2 answers
  <?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> 
+5
source

Basic example - it works for me

PHP ANSWERS

 $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 } } }); 
+4
source

Source: https://habr.com/ru/post/912978/


All Articles