发送php响应给ajax

前端之家收集整理的这篇文章主要介绍了发送php响应给ajax前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,我有一个 PHP脚本,结尾如此:
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);

还有一些jquery,我计划在我的脚本运行时提醒我.

jQuery(document).ready(function() {

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

});

编辑来源

<?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>
原文链接:https://www.f2er.com/php/130270.html

猜你在找的PHP相关文章