Ajax响应返回undefined

时间:2015-06-30 15:42:22

标签: php jquery ajax codeigniter pusher

您好我多次使用ajax。但是这次我无法弄清楚我的代码是什么问题。我的脑袋在2天后爆炸了。我正在使用推送器在CodeIgniter中进行实时通知。这是我的代码。

  $(document).on("click", ".myonoffswitch", function () {
    if (confirm("Sure!!! You want to Change the post status?"))
    {
        var thiss = $(this);
        var prod_id = $(this).attr('id');
        var status = $(this).attr('value');
        var tdid = $(this).parent().prev().attr('class');
        var td = $(this).parent().prev();

        var adsstatus = $.ajax({type: "POST", dataType: "json", url: base_url + 'init/ads_status', data: {'prod_id': prod_id, 'status': status}});
        $.when(adsstatus).done(function (adsstatuss) {
            var msg;
            msg = adsstatuss;
            alert(msg.length);
            var updated_status = msg[0].post_status;
            alert(updated_status);
            //alert(updated_status);
            if (updated_status == "0" && tdid == prod_id) {
                td.css("background", "#ccffcc");
                td.text("On");
                thiss.val(updated_status);


            }
            if (updated_status == "1" && tdid == prod_id) {
                td.css("background", "#ffcccc");
                td.text("Off");
                thiss.val(updated_status);
            }
        });
    }
});

ads_status(CodeIgniter中的方法)

 public function ads_status() {
    $prod_id = $_POST['prod_id'];
    $status = $_POST['status'];
    if ($status == "1") {
        $new_status = "0";
        $this->cmsdbmodel->ads_status($prod_id, $new_status);
        $dat = $this->cmsdbmodel->get_updated_status($prod_id);
        $ads_type = "product";
        $emailreturn = $this->send_email_ads_status($prod_id, $ads_type);


        //send user alert of product//
        if ($dat[0]->post_status == "0") {
            $lastInsertedId = $prod_id;
            $modelNBrandId = $this->dbmodel->checkProductContainsAlert($lastInsertedId);
            $getAlertEmail = $this->dbmodel->getAlertEmailFromBrandNModelId($modelNBrandId);
            if (!empty($getAlertEmail)) {
                foreach ($getAlertEmail as $alerts) {
                    $alertsEmail = $alerts->email;
                }
            }
            $alertsPost = $this->dbmodel->getPostAlerts($lastInsertedId);
            $sendPostAlerts = json_encode($alertsPost);
            $encrytpEmail = hash('sha256', $alertsEmail);
            $this->pusher->trigger($encrytpEmail, 'alerts', $sendPostAlerts);
        }
        //send user alert of product//

        $realtime = $this->realTime_charts();
        $realtime["post_status"] = $dat[0]->post_status;
        $realtime["emailres"] = $emailreturn;
        $data = $realtime;
        $this->pusher->trigger('recent_activity', 'new_event1', $data);
        echo json_encode($data);
    }
 }

这里我想在pusher中触发事件以及echo json对象。但在ajax中,回复是undefined。同样在控制台中我可以看到json,但在提醒响应时它显示undefined。我的代码中存在问题。请帮我。

0 个答案:

没有答案