在$ .ajax中传递数据时出现500内部服务器错误

时间:2015-11-20 06:28:36

标签: php ajax

在进行ajax调用时,我收到500内部服务器错误。

$(document).ready(function(){
    $("#txtSearch").change(function(){
        var search = $("#txtSearch").val();
        var str = 'search=' + search;
        $.ajax({
            type: "POST",
            url: "searchProcess.php",
            data: str,
            dataType: "json",
            success: function(data) {
                if (data["type"] == "1") {                     
                    alert('Please Enter Value To Search');

                }else if (data['type'] == "2") {
                        alert('No Such of This Event In Database');


                }else if (data['type'] == "3")  {
                    $('#gallery').hide().html(data['data']).fadeIn('5s');
                }
            }
        });
    });
});

searchProcess.php

if (!isset($_POST["search"])) {
    print json_encode(['type'=>'1']);

}else{

    $seValue = "%{$_POST['search']}%";

    $querySel     = "SELECT * FROM gallery WHERE galleryTitle LIKE :title OR date LIKE :dat";
    $stmtquerySel = $conn->prepare($querySel);
    $stmtquerySel->bindParam('title',$seValue);
    $stmtquerySel->bindParam('dat',$seValue);
    $stmtquerySel->execute();

    if ($stmtquerySel->rowCount() == 0) {

        print json_encode(['type'=>'2']);

    }else{

   $galleryGrid = "";
        while ($rowQuerySel = $stmtquerySel->fetch()) {

            $id    = $rowQuerySel['galleryId'];
            $image = $rowQuerySel['image'];
            $title = $rowQuerySel['galleryTitle'];
            $date  = $rowQuerySel['date'];

            $galleryGrid .= "<div class='col-lg-4 col-sm-6'>
                            <div class=''>
                                <a href='gallerydetails.php?id=$id'>

                                 <img src='img/$image' width='100%'>

                                 <div>Event Name:$title</div>
                                <div>Date :$date</div>
                                 </br></br>


                                </a>
                                 </div>      

                        </div> 


            ";
        }
        $galleryGrid .="<div class='col-md-11 text-center'>
              <button type='submit' class='btn btn-primary btn-lg' onClick=location.href='gallery.php'>Back</button>
            </div>";

        print json_encode(['type'=>'3', 'data'=>$galleryGrid]);
    }
}

当我在localhost上试用它时会运行,但上传到server时会出现500内部错误

1 个答案:

答案 0 :(得分:0)

使用此

 print json_encode(array('type'=>'3', 'data'=>$galleryGrid));

而不是

print json_encode(['type'=>'3', 'data'=>$galleryGrid]);