dongzhitao4839 2015-01-22 20:48
浏览 75

PHP json响应不起作用

I am doing an ajax update. I just want a response of success or failure so I know how to handle some things in the front end. Problem is it isn't working. Brand new to all of this.

$('.delete-template-popup .confirm').on('click', function() {
    var templateName = $('.loaded-template-name').html();

    var templateArray = {};

    templateArray.templateName = templateName;

    var JSONObject = [templateArray];
    var templateJson = JSON.stringify(JSONObject);

    $.ajax({
        url: 'http://localhost/ds-layouts/public/delete-template.php',
        type: 'post',
        data: {"templatePHP" : templateJson},
        success: function(data) {
            console.log(data)
            if (data.status == "success") {
                console.log(1)
            }
            // if (data.status == "success") {
            //  closePopup($('.delete-template-popup'));
            //  window.location.replace("http:////localhost/ds-layouts/public/manage-layouts.php");
            // } else {
            //  $('.delete-template-popup .error').show().html('An error occurred processing your request.  Please try again.  If this error persists contact blah.');
            // }                        
        }
    });
});

and the php

if ($flag) {
        //mysqli_commit($connection);
        if ($debug) {
            echo "pass";
            echo "
";
        }
        //$_SESSION["message"] = "Template delete was successful.";
        //header('Content-Type: application/json');
        header('Content-Type: application/json; charset=UTF8');
        echo json_encode(array('status' => 'success'));
    } else {
        if ($debug) {
            echo "fail";
            echo "
";
        }
        //mysqli_rollback($connection);
        // header('Content-Type: application/json');
        // echo json_encode(array('status' => 'failure'));
    }

So the deal is I am getting into the if block of the php statment fine. If I have the header part of the block I get all of my echo statements passed properly and I can read them in Chromes developer console. The moment I uncomment the header statement nothing works. This is for either one of $flag cases true or false.

I have this same type of script in another area and it works absolutely fine. Don't mind the comments. I was just commenting things out as a way to figure out where things were breaking. That is how I determened the header was causing it.

  • 写回答

3条回答 默认 最新

  • douju4278 2015-01-22 21:04
    关注

    Maybe adding the dataType: "json", to your AJAX request object will help?

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。