发送php响应到ajax [英] Send php response to ajax

查看:111
本文介绍了发送php响应到ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个php脚本,其结束是这样的:

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);

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

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");
                }
    });

});

编辑源

推荐答案

        <?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>

这篇关于发送php响应到ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆